fmt; fixed IP address dialer option for seclient

This commit is contained in:
Vladislav Yarmak 2021-03-27 02:48:07 +02:00
parent 9a35f96795
commit 1c4a8991bc
3 changed files with 58 additions and 23 deletions

31
fixed.go Normal file
View file

@ -0,0 +1,31 @@
package main
import (
"context"
"net"
)
type FixedDialer struct {
fixedAddress string
next ContextDialer
}
func NewFixedDialer(address string, next ContextDialer) *FixedDialer {
return &FixedDialer{
fixedAddress: address,
next: next,
}
}
func (d *FixedDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
_, port, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
return d.next.DialContext(ctx, network, net.JoinHostPort(d.fixedAddress, port))
}
func (d *FixedDialer) Dial(network, address string) (net.Conn, error) {
return d.DialContext(context.Background(), network, address)
}

22
main.go
View file

@ -1,8 +1,9 @@
package main package main
import ( import (
"encoding/csv"
"context" "context"
"crypto/tls"
"encoding/csv"
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
@ -11,9 +12,8 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"time"
"crypto/tls"
"strings" "strings"
"time"
xproxy "golang.org/x/net/proxy" xproxy "golang.org/x/net/proxy"
@ -43,11 +43,11 @@ type CLIArgs struct {
bindAddress string bindAddress string
verbosity int verbosity int
timeout time.Duration timeout time.Duration
resolver string
showVersion bool showVersion bool
proxy string proxy string
apiLogin string apiLogin string
apiPassword string apiPassword string
apiAddress string
} }
func parse_args() CLIArgs { func parse_args() CLIArgs {
@ -59,15 +59,13 @@ func parse_args() CLIArgs {
flag.IntVar(&args.verbosity, "verbosity", 20, "logging verbosity "+ flag.IntVar(&args.verbosity, "verbosity", 20, "logging verbosity "+
"(10 - debug, 20 - info, 30 - warning, 40 - error, 50 - critical)") "(10 - debug, 20 - info, 30 - warning, 40 - error, 50 - critical)")
flag.DurationVar(&args.timeout, "timeout", 10*time.Second, "timeout for network operations") flag.DurationVar(&args.timeout, "timeout", 10*time.Second, "timeout for network operations")
flag.StringVar(&args.resolver, "resolver", "https://cloudflare-dns.com/dns-query",
"DNS/DoH/DoT resolver to workaround Hola blocked hosts. "+
"See https://github.com/ameshkov/dnslookup/ for upstream DNS URL format.")
flag.BoolVar(&args.showVersion, "version", false, "show program version and exit") flag.BoolVar(&args.showVersion, "version", false, "show program version and exit")
flag.StringVar(&args.proxy, "proxy", "", "sets base proxy to use for all dial-outs. "+ flag.StringVar(&args.proxy, "proxy", "", "sets base proxy to use for all dial-outs. "+
"Format: <http|https|socks5|socks5h>://[login:password@]host[:port] "+ "Format: <http|https|socks5|socks5h>://[login:password@]host[:port] "+
"Examples: http://user:password@192.168.1.1:3128, socks5://10.0.0.1:1080") "Examples: http://user:password@192.168.1.1:3128, socks5://10.0.0.1:1080")
flag.StringVar(&args.apiLogin, "api-login", "se0316", "SurfEasy API login") flag.StringVar(&args.apiLogin, "api-login", "se0316", "SurfEasy API login")
flag.StringVar(&args.apiPassword, "api-password", "SILrMEPBmJuhomxWkfm3JalqHX2Eheg1YhlEZiMh8II", "SurfEasy API password") flag.StringVar(&args.apiPassword, "api-password", "SILrMEPBmJuhomxWkfm3JalqHX2Eheg1YhlEZiMh8II", "SurfEasy API password")
flag.StringVar(&args.apiAddress, "api-address", "", "override IP address of api.sec-tunnel.com")
flag.Parse() flag.Parse()
if args.country == "" { if args.country == "" {
arg_fail("Country can't be empty string.") arg_fail("Country can't be empty string.")
@ -110,6 +108,7 @@ func run() int {
Timeout: 30 * time.Second, Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second, KeepAlive: 30 * time.Second,
} }
if args.proxy != "" { if args.proxy != "" {
xproxy.RegisterDialerType("http", proxyFromURLWrapper) xproxy.RegisterDialerType("http", proxyFromURLWrapper)
xproxy.RegisterDialerType("https", proxyFromURLWrapper) xproxy.RegisterDialerType("https", proxyFromURLWrapper)
@ -126,6 +125,11 @@ func run() int {
dialer = pxDialer.(ContextDialer) dialer = pxDialer.(ContextDialer)
} }
seclientDialer := dialer
if args.apiAddress != "" {
seclientDialer = NewFixedDialer(args.apiAddress, dialer)
}
// Dialing w/o SNI, receiving self-signed certificate, so skip verification. // Dialing w/o SNI, receiving self-signed certificate, so skip verification.
// Either way we'll validate certificate of actual proxy server. // Either way we'll validate certificate of actual proxy server.
tlsConfig := &tls.Config{ tlsConfig := &tls.Config{
@ -133,9 +137,9 @@ func run() int {
InsecureSkipVerify: true, InsecureSkipVerify: true,
} }
seclient, err := se.NewSEClient(args.apiLogin, args.apiPassword, &http.Transport{ seclient, err := se.NewSEClient(args.apiLogin, args.apiPassword, &http.Transport{
DialContext: dialer.DialContext, DialContext: seclientDialer.DialContext,
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) { DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
conn, err := dialer.DialContext(ctx, network, addr) conn, err := seclientDialer.DialContext(ctx, network, addr)
if err != nil { if err != nil {
return conn, err return conn, err
} }

View file

@ -2,9 +2,9 @@ package seclient
import ( import (
"encoding/json" "encoding/json"
"net"
"fmt"
"errors" "errors"
"fmt"
"net"
"strconv" "strconv"
) )