mirror of
https://github.com/SajadMRjl/find-me-internet.git
synced 2026-07-13 13:56:26 +00:00
fix: add config and logger, cleanup
This commit is contained in:
@@ -3,6 +3,7 @@ package parser
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
|
||||
"find-me-internet/internal/model"
|
||||
@@ -10,11 +11,13 @@ import (
|
||||
"github.com/gvcgo/vpnparser/pkgs/outbound"
|
||||
)
|
||||
|
||||
// tempConfig allows us to extract deep fields from the Sing-box JSON
|
||||
// tempConfig covers multiple locations where SNI might be hidden in the JSON
|
||||
type tempConfig struct {
|
||||
Transport struct {
|
||||
Type string `json:"type"`
|
||||
} `json:"transport"`
|
||||
|
||||
// Standard TLS
|
||||
TLS struct {
|
||||
ServerName string `json:"server_name"`
|
||||
} `json:"tls"`
|
||||
@@ -26,22 +29,42 @@ func ParseLink(raw string) (*model.Proxy, error) {
|
||||
return nil, fmt.Errorf("empty link")
|
||||
}
|
||||
|
||||
// 1. Parse Raw Link
|
||||
// We omit the second argument to let the library use default parsing
|
||||
// 1. Parse using library
|
||||
item := outbound.ParseRawUriToProxyItem(raw)
|
||||
if item == nil {
|
||||
return nil, fmt.Errorf("unknown protocol or invalid link")
|
||||
return nil, fmt.Errorf("invalid link")
|
||||
}
|
||||
|
||||
// 2. Initialize Proxy Model
|
||||
p := &model.Proxy{
|
||||
RawLink: raw,
|
||||
Address: item.Address,
|
||||
Port: item.Port,
|
||||
}
|
||||
|
||||
// 3. Extract SNI and Network from the Outbound JSON
|
||||
// The library packs the details into 'item.Outbound' string
|
||||
// 2. Clean up Protocol (Fixing the "vless://" bug)
|
||||
// Some versions of the lib return "vless://" instead of "vless"
|
||||
scheme := strings.ToLower(item.Scheme)
|
||||
scheme = strings.TrimSuffix(scheme, "://")
|
||||
|
||||
switch scheme {
|
||||
case "vless":
|
||||
p.Type = model.TypeVLESS
|
||||
if strings.Contains(raw, "reality") {
|
||||
p.Type = model.TypeVLESS
|
||||
}
|
||||
case "vmess":
|
||||
p.Type = model.TypeVMess
|
||||
case "trojan":
|
||||
p.Type = model.TypeTrojan
|
||||
case "ss", "shadowsocks":
|
||||
p.Type = model.TypeShadowsocks
|
||||
default:
|
||||
p.Type = model.TypeUnknown
|
||||
slog.Warn("Unknown protocol", "scheme", scheme, "raw", item.Scheme)
|
||||
}
|
||||
|
||||
// 3. Extract SNI (Fixing the "sni=" bug)
|
||||
// We first try to get it from the standard fields
|
||||
if item.Outbound != "" {
|
||||
var cfg tempConfig
|
||||
if err := json.Unmarshal([]byte(item.Outbound), &cfg); err == nil {
|
||||
@@ -49,26 +72,48 @@ func ParseLink(raw string) (*model.Proxy, error) {
|
||||
p.SNI = cfg.TLS.ServerName
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: If JSON extraction failed but we have a generic "Host" (sometimes used as SNI)
|
||||
// Note: 'item.Host' doesn't exist either, so we rely solely on JSON extraction above.
|
||||
|
||||
// 4. Map Protocol (Library uses 'Scheme')
|
||||
switch strings.ToLower(item.Scheme) {
|
||||
case "vless":
|
||||
p.Type = model.TypeVLESS
|
||||
if strings.Contains(raw, "reality") {
|
||||
p.Type = model.TypeVLESS // Reality is technically VLESS
|
||||
// 4. Fallback for SNI (Crucial for Reality/VLESS)
|
||||
// If JSON extraction failed, try to parse the raw URL query parameters manually.
|
||||
// This is often more reliable than the JSON dump for simple fields.
|
||||
if p.SNI == "" {
|
||||
// Quick and dirty manual check for "&sni=..." or "&peer=..."
|
||||
if val := extractQueryParam(raw, "sni"); val != "" {
|
||||
p.SNI = val
|
||||
} else if val := extractQueryParam(raw, "peer"); val != "" {
|
||||
p.SNI = val // "peer" is often used in Telegram proxies as SNI
|
||||
} else if val := extractQueryParam(raw, "host"); val != "" {
|
||||
p.SNI = val
|
||||
}
|
||||
case "vmess":
|
||||
p.Type = model.TypeVMess
|
||||
case "trojan":
|
||||
p.Type = model.TypeTrojan
|
||||
case "shadowsocks", "ss":
|
||||
p.Type = model.TypeShadowsocks
|
||||
default:
|
||||
p.Type = model.TypeUnknown
|
||||
}
|
||||
|
||||
// 5. Final Safety: Reality MUST have an SNI
|
||||
// If we still don't have one, the TLS check will inevitably fail.
|
||||
// We can warn here.
|
||||
if p.Type == model.TypeVLESS && p.SNI == "" {
|
||||
slog.Debug("Warning: VLESS proxy has no SNI", "addr", p.Address)
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Helper to manually grab query params from the raw string
|
||||
// because sometimes the parser library logic is opaque.
|
||||
func extractQueryParam(url, key string) string {
|
||||
// Find "key="
|
||||
keyStr := key + "="
|
||||
start := strings.Index(url, keyStr)
|
||||
if start == -1 {
|
||||
return ""
|
||||
}
|
||||
// Move to value start
|
||||
start += len(keyStr)
|
||||
|
||||
// Find end of value (either '&' or '#')
|
||||
rest := url[start:]
|
||||
end := strings.IndexAny(rest, "&#")
|
||||
if end == -1 {
|
||||
return rest
|
||||
}
|
||||
return rest[:end]
|
||||
}
|
||||
Reference in New Issue
Block a user