mirror of
https://github.com/SajadMRjl/find-me-internet.git
synced 2026-07-13 13:56:26 +00:00
fix: readable log messages
This commit is contained in:
26
cmd/main.go
26
cmd/main.go
@@ -54,32 +54,32 @@ func main() {
|
|||||||
// Step A: Parse
|
// Step A: Parse
|
||||||
proxy, err := parser.ParseLink(raw)
|
proxy, err := parser.ParseLink(raw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Debug("Parse failed", "err", err)
|
// Parser logs its own errors at DEBUG level
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step B: Cheap Filter (TCP/TLS)
|
// Step B: Network Filter
|
||||||
if !netFilter.Check(proxy) {
|
if !netFilter.Check(proxy) {
|
||||||
// Failed cheap checks, discard silently or debug
|
// Filter logs specific failure reason (TCP/TLS) at DEBUG level
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step C: Expensive Test (Sing-box)
|
// Step C: Integration Test
|
||||||
semaphore <- struct{}{} // Acquire lock
|
semaphore <- struct{}{}
|
||||||
err = boxRunner.Test(proxy)
|
err = boxRunner.Test(proxy)
|
||||||
<-semaphore // Release lock
|
<-semaphore
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Debug("Test failed", "proxy", proxy.Address, "err", err)
|
// Runner logs probe failures
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Success!
|
// Success Log
|
||||||
validCount++
|
slog.Info("proxy_verified",
|
||||||
slog.Info("Valid Proxy Found",
|
"target", proxy.Address,
|
||||||
"addr", proxy.Address,
|
"protocol", proxy.Type,
|
||||||
"latency", proxy.Latency.Milliseconds(),
|
"latency_ms", proxy.Latency.Milliseconds(),
|
||||||
"type", proxy.Type,
|
"sni", proxy.SNI,
|
||||||
)
|
)
|
||||||
|
|
||||||
}(link)
|
}(link)
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
"find-me-internet/internal/model"
|
"find-me-internet/internal/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Pipeline manages the "Cheap Check" logic
|
|
||||||
type Pipeline struct {
|
type Pipeline struct {
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
}
|
}
|
||||||
@@ -19,31 +18,42 @@ func NewPipeline(timeout time.Duration) *Pipeline {
|
|||||||
return &Pipeline{Timeout: timeout}
|
return &Pipeline{Timeout: timeout}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check runs a sequence of low-cost network tests.
|
|
||||||
// Returns false immediately if any stage fails.
|
|
||||||
func (f *Pipeline) Check(p *model.Proxy) bool {
|
func (f *Pipeline) Check(p *model.Proxy) bool {
|
||||||
// 1. Sanity Check
|
target := net.JoinHostPort(p.Address, strconv.Itoa(p.Port))
|
||||||
if p.Address == "" || p.Port == 0 {
|
log := slog.With("target", target, "protocol", p.Type)
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. TCP Liveness (Is the port open?)
|
// 1. TCP Connectivity
|
||||||
|
start := time.Now()
|
||||||
if !f.checkTCP(p) {
|
if !f.checkTCP(p) {
|
||||||
slog.Debug("TCP Connection failed", "addr", p.Address, "port", p.Port)
|
log.Debug("tcp_connect_failed", "duration", time.Since(start))
|
||||||
p.IsOnline = false
|
p.IsOnline = false
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
p.IsOnline = true
|
p.IsOnline = true
|
||||||
|
|
||||||
// 3. TLS Validity (Does it handshake?)
|
// 2. TLS Handshake
|
||||||
// Only required if SNI is present or port is standard HTTPS
|
// Only proceed if protocol supports/requires TLS
|
||||||
if p.SNI != "" || p.Port == 443 {
|
shouldCheckTLS := p.SNI != "" || p.Port == 443 || p.Type == model.TypeVLESS || p.Type == model.TypeTrojan
|
||||||
if !f.checkTLS(p) {
|
|
||||||
slog.Debug("TLS Handshake failed", "addr", p.Address, "sni", p.SNI)
|
if shouldCheckTLS {
|
||||||
|
sni := p.SNI
|
||||||
|
if sni == "" {
|
||||||
|
sni = p.Address // Fallback for handshake
|
||||||
|
}
|
||||||
|
|
||||||
|
startTLS := time.Now()
|
||||||
|
if !f.checkTLS(p, sni) {
|
||||||
|
log.Debug("tls_handshake_failed",
|
||||||
|
"sni", sni,
|
||||||
|
"duration", time.Since(startTLS),
|
||||||
|
)
|
||||||
p.IsTLSSecure = false
|
p.IsTLSSecure = false
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
p.IsTLSSecure = true
|
p.IsTLSSecure = true
|
||||||
|
log.Debug("network_checks_passed", "duration", time.Since(start))
|
||||||
|
} else {
|
||||||
|
log.Debug("network_checks_passed", "note", "tls_skipped_no_sni")
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
@@ -59,20 +69,13 @@ func (f *Pipeline) checkTCP(p *model.Proxy) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Pipeline) checkTLS(p *model.Proxy) bool {
|
func (f *Pipeline) checkTLS(p *model.Proxy, sni string) bool {
|
||||||
address := net.JoinHostPort(p.Address, strconv.Itoa(p.Port))
|
address := net.JoinHostPort(p.Address, strconv.Itoa(p.Port))
|
||||||
dialer := &net.Dialer{Timeout: f.Timeout}
|
dialer := &net.Dialer{Timeout: f.Timeout}
|
||||||
|
|
||||||
// We skip verification because many proxies use self-signed certs or Reality.
|
|
||||||
// The goal is to check if the server *speaks* TLS, not if the cert is trusted by Root CAs.
|
|
||||||
conf := &tls.Config{
|
conf := &tls.Config{
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
ServerName: p.SNI,
|
ServerName: sni,
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback SNI if none provided
|
|
||||||
if conf.ServerName == "" {
|
|
||||||
conf.ServerName = p.Address
|
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := tls.DialWithDialer(dialer, "tcp", address, conf)
|
conn, err := tls.DialWithDialer(dialer, "tcp", address, conf)
|
||||||
@@ -11,13 +11,10 @@ import (
|
|||||||
"github.com/gvcgo/vpnparser/pkgs/outbound"
|
"github.com/gvcgo/vpnparser/pkgs/outbound"
|
||||||
)
|
)
|
||||||
|
|
||||||
// tempConfig covers multiple locations where SNI might be hidden in the JSON
|
|
||||||
type tempConfig struct {
|
type tempConfig struct {
|
||||||
Transport struct {
|
Transport struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
} `json:"transport"`
|
} `json:"transport"`
|
||||||
|
|
||||||
// Standard TLS
|
|
||||||
TLS struct {
|
TLS struct {
|
||||||
ServerName string `json:"server_name"`
|
ServerName string `json:"server_name"`
|
||||||
} `json:"tls"`
|
} `json:"tls"`
|
||||||
@@ -29,10 +26,11 @@ func ParseLink(raw string) (*model.Proxy, error) {
|
|||||||
return nil, fmt.Errorf("empty link")
|
return nil, fmt.Errorf("empty link")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. Parse using library
|
// 1. Library Parse
|
||||||
item := outbound.ParseRawUriToProxyItem(raw)
|
item := outbound.ParseRawUriToProxyItem(raw)
|
||||||
if item == nil {
|
if item == nil {
|
||||||
return nil, fmt.Errorf("invalid link")
|
slog.Debug("parser_rejected_link", "reason", "invalid_structure", "raw_prefix", raw[:min(20, len(raw))])
|
||||||
|
return nil, fmt.Errorf("invalid link structure")
|
||||||
}
|
}
|
||||||
|
|
||||||
p := &model.Proxy{
|
p := &model.Proxy{
|
||||||
@@ -41,11 +39,10 @@ func ParseLink(raw string) (*model.Proxy, error) {
|
|||||||
Port: item.Port,
|
Port: item.Port,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Clean up Protocol (Fixing the "vless://" bug)
|
// 2. Protocol Normalization
|
||||||
// Some versions of the lib return "vless://" instead of "vless"
|
|
||||||
scheme := strings.ToLower(item.Scheme)
|
scheme := strings.ToLower(item.Scheme)
|
||||||
scheme = strings.TrimSuffix(scheme, "://")
|
scheme = strings.TrimSuffix(scheme, "://")
|
||||||
|
|
||||||
switch scheme {
|
switch scheme {
|
||||||
case "vless":
|
case "vless":
|
||||||
p.Type = model.TypeVLESS
|
p.Type = model.TypeVLESS
|
||||||
@@ -60,60 +57,62 @@ func ParseLink(raw string) (*model.Proxy, error) {
|
|||||||
p.Type = model.TypeShadowsocks
|
p.Type = model.TypeShadowsocks
|
||||||
default:
|
default:
|
||||||
p.Type = model.TypeUnknown
|
p.Type = model.TypeUnknown
|
||||||
slog.Warn("Unknown protocol", "scheme", scheme, "raw", item.Scheme)
|
slog.Warn("unknown_protocol_detected", "scheme", scheme, "target", p.Address)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Extract SNI (Fixing the "sni=" bug)
|
// 3. Deep Extraction (JSON)
|
||||||
// We first try to get it from the standard fields
|
extractionSource := "none"
|
||||||
if item.Outbound != "" {
|
if item.Outbound != "" {
|
||||||
var cfg tempConfig
|
var cfg tempConfig
|
||||||
if err := json.Unmarshal([]byte(item.Outbound), &cfg); err == nil {
|
if err := json.Unmarshal([]byte(item.Outbound), &cfg); err == nil {
|
||||||
p.Network = cfg.Transport.Type
|
p.Network = cfg.Transport.Type
|
||||||
p.SNI = cfg.TLS.ServerName
|
p.SNI = cfg.TLS.ServerName
|
||||||
|
if p.Network != "" || p.SNI != "" {
|
||||||
|
extractionSource = "json_config"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Fallback for SNI (Crucial for Reality/VLESS)
|
// 4. Fallback Extraction (Query Params)
|
||||||
// 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 == "" {
|
if p.SNI == "" {
|
||||||
// Quick and dirty manual check for "&sni=..." or "&peer=..."
|
|
||||||
if val := extractQueryParam(raw, "sni"); val != "" {
|
if val := extractQueryParam(raw, "sni"); val != "" {
|
||||||
p.SNI = val
|
p.SNI = val
|
||||||
} else if val := extractQueryParam(raw, "peer"); val != "" {
|
extractionSource = "query_sni"
|
||||||
p.SNI = val // "peer" is often used in Telegram proxies as SNI
|
|
||||||
} else if val := extractQueryParam(raw, "host"); val != "" {
|
} else if val := extractQueryParam(raw, "host"); val != "" {
|
||||||
p.SNI = val
|
p.SNI = val
|
||||||
|
extractionSource = "query_host"
|
||||||
|
} else if val := extractQueryParam(raw, "peer"); val != "" {
|
||||||
|
p.SNI = val
|
||||||
|
extractionSource = "query_peer"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. Final Safety: Reality MUST have an SNI
|
slog.Debug("proxy_parsed",
|
||||||
// If we still don't have one, the TLS check will inevitably fail.
|
"target", fmt.Sprintf("%s:%d", p.Address, p.Port),
|
||||||
// We can warn here.
|
"protocol", p.Type,
|
||||||
if p.Type == model.TypeVLESS && p.SNI == "" {
|
"sni", p.SNI,
|
||||||
slog.Debug("Warning: VLESS proxy has no SNI", "addr", p.Address)
|
"sni_source", extractionSource,
|
||||||
}
|
)
|
||||||
|
|
||||||
return p, nil
|
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 {
|
func extractQueryParam(url, key string) string {
|
||||||
// Find "key="
|
|
||||||
keyStr := key + "="
|
keyStr := key + "="
|
||||||
start := strings.Index(url, keyStr)
|
start := strings.Index(url, keyStr)
|
||||||
if start == -1 {
|
if start == -1 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
// Move to value start
|
|
||||||
start += len(keyStr)
|
start += len(keyStr)
|
||||||
|
|
||||||
// Find end of value (either '&' or '#')
|
|
||||||
rest := url[start:]
|
rest := url[start:]
|
||||||
end := strings.IndexAny(rest, "&#")
|
end := strings.IndexAny(rest, "&#")
|
||||||
if end == -1 {
|
if end == -1 {
|
||||||
return rest
|
return rest
|
||||||
}
|
}
|
||||||
return rest[:end]
|
return rest[:end]
|
||||||
|
}
|
||||||
|
|
||||||
|
func min(a, b int) int {
|
||||||
|
if a < b { return a }
|
||||||
|
return b
|
||||||
}
|
}
|
||||||
@@ -22,60 +22,62 @@ type Runner struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewRunner(binPath, testURL string, timeout time.Duration) *Runner {
|
func NewRunner(binPath, testURL string, timeout time.Duration) *Runner {
|
||||||
return &Runner{
|
return &Runner{BinPath: binPath, TestURL: testURL, Timeout: timeout}
|
||||||
BinPath: binPath,
|
|
||||||
TestURL: testURL,
|
|
||||||
Timeout: timeout,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test spins up a Sing-box instance and measures HTTP latency
|
|
||||||
func (r *Runner) Test(p *model.Proxy) error {
|
func (r *Runner) Test(p *model.Proxy) error {
|
||||||
// 1. Acquire Local Port
|
log := slog.With("target", p.Address, "sni", p.SNI)
|
||||||
|
|
||||||
|
// 1. Port Allocation
|
||||||
port, err := getFreePort()
|
port, err := getFreePort()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get port: %w", err)
|
log.Error("local_port_allocation_failed", "error", err)
|
||||||
}
|
return err
|
||||||
|
}
|
||||||
// 2. Generate Configuration
|
|
||||||
configData, err := GenerateConfig(p, port)
|
// 2. Config Generation
|
||||||
if err != nil {
|
configData, err := GenerateConfig(p, port)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("config_generation_failed", "error", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Write Config File
|
|
||||||
// Using a unique name prevents collisions in concurrent tests
|
|
||||||
configName := filepath.Join(os.TempDir(), fmt.Sprintf("sb_%d_%s.json", port, p.Address))
|
configName := filepath.Join(os.TempDir(), fmt.Sprintf("sb_%d_%s.json", port, p.Address))
|
||||||
if err := os.WriteFile(configName, configData, 0644); err != nil {
|
if err := os.WriteFile(configName, configData, 0644); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer os.Remove(configName) // Cleanup
|
defer os.Remove(configName)
|
||||||
|
|
||||||
// 4. Execute Sing-box
|
// 3. Process Execution
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), r.Timeout+2*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), r.Timeout+2*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
cmd := exec.CommandContext(ctx, r.BinPath, "run", "-c", configName)
|
cmd := exec.CommandContext(ctx, r.BinPath, "run", "-c", configName)
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
return fmt.Errorf("startup failed: %w", err)
|
log.Error("singbox_process_start_failed", "error", err)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure cleanup happens even if panic occurs
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if cmd.Process != nil {
|
if cmd.Process != nil {
|
||||||
_ = cmd.Process.Kill()
|
cmd.Process.Kill()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// 5. Wait for Binding
|
// 4. Wait for Binding
|
||||||
if !waitForPort(port, 2*time.Second) {
|
if !waitForPort(port, 2*time.Second) {
|
||||||
return fmt.Errorf("sing-box failed to bind port %d", port)
|
log.Debug("singbox_bind_timeout", "local_port", port)
|
||||||
|
return fmt.Errorf("process_bind_timeout")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6. HTTP Latency Test
|
// 5. HTTP Probe
|
||||||
|
startProbe := time.Now()
|
||||||
latency, err := r.measureLatency(port)
|
latency, err := r.measureLatency(port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Debug("Latency test failed", "err", err, "proxy", p.Address)
|
log.Debug("http_probe_failed",
|
||||||
|
"duration", time.Since(startProbe),
|
||||||
|
"error", err,
|
||||||
|
)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,11 +87,10 @@ func (r *Runner) Test(p *model.Proxy) error {
|
|||||||
|
|
||||||
func (r *Runner) measureLatency(port int) (time.Duration, error) {
|
func (r *Runner) measureLatency(port int) (time.Duration, error) {
|
||||||
proxyUrl, _ := url.Parse(fmt.Sprintf("http://127.0.0.1:%d", port))
|
proxyUrl, _ := url.Parse(fmt.Sprintf("http://127.0.0.1:%d", port))
|
||||||
|
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
Proxy: http.ProxyURL(proxyUrl),
|
Proxy: http.ProxyURL(proxyUrl),
|
||||||
DisableKeepAlives: true, // Force new connection
|
DisableKeepAlives: true,
|
||||||
},
|
},
|
||||||
Timeout: r.Timeout,
|
Timeout: r.Timeout,
|
||||||
}
|
}
|
||||||
@@ -101,23 +102,18 @@ func (r *Runner) measureLatency(port int) (time.Duration, error) {
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != 200 && resp.StatusCode != 204 {
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||||
return 0, fmt.Errorf("invalid status code: %d", resp.StatusCode)
|
return 0, fmt.Errorf("unexpected_status_code_%d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
return time.Since(start), nil
|
return time.Since(start), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getFreePort asks the kernel for a random open port
|
|
||||||
func getFreePort() (int, error) {
|
func getFreePort() (int, error) {
|
||||||
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
||||||
if err != nil {
|
if err != nil { return 0, err }
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
l, err := net.ListenTCP("tcp", addr)
|
l, err := net.ListenTCP("tcp", addr)
|
||||||
if err != nil {
|
if err != nil { return 0, err }
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
defer l.Close()
|
defer l.Close()
|
||||||
return l.Addr().(*net.TCPAddr).Port, nil
|
return l.Addr().(*net.TCPAddr).Port, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user