Files
opera-proxy/main.go
T

401 lines
12 KiB
Go
Raw Normal View History

2021-03-25 20:45:48 +02:00
package main
import (
2024-08-01 12:01:24 +03:00
"bytes"
2021-03-27 00:43:33 +02:00
"context"
"crypto/tls"
"crypto/x509"
"encoding/csv"
2021-03-26 22:34:43 +02:00
"errors"
"flag"
"fmt"
2024-08-01 12:01:24 +03:00
"io"
"io/ioutil"
2021-03-25 20:45:48 +02:00
"log"
2021-03-26 22:34:43 +02:00
"net"
"net/http"
2024-08-01 12:44:23 +03:00
"net/netip"
2021-03-26 22:34:43 +02:00
"net/url"
"os"
"strings"
"time"
2021-03-25 20:45:48 +02:00
2021-03-26 22:34:43 +02:00
xproxy "golang.org/x/net/proxy"
2021-03-27 00:43:33 +02:00
se "github.com/Snawoot/opera-proxy/seclient"
2021-03-25 20:45:48 +02:00
)
2021-03-28 22:21:23 +03:00
const (
2024-10-05 19:01:39 +03:00
API_DOMAIN = "api2.sec-tunnel.com"
2021-03-28 22:21:23 +03:00
PROXY_SUFFIX = "sec-tunnel.com"
)
2021-03-26 22:34:43 +02:00
var (
2021-03-26 22:40:27 +02:00
version = "undefined"
2021-03-25 20:45:48 +02:00
)
2021-03-26 22:34:43 +02:00
func perror(msg string) {
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, msg)
}
func arg_fail(msg string) {
perror(msg)
perror("Usage:")
flag.PrintDefaults()
os.Exit(2)
}
2024-08-01 12:01:24 +03:00
type CSVArg struct {
values []string
}
func (a *CSVArg) String() string {
if len(a.values) == 0 {
return ""
}
buf := new(bytes.Buffer)
wr := csv.NewWriter(buf)
wr.Write(a.values)
wr.Flush()
return strings.TrimRight(buf.String(), "\n")
}
func (a *CSVArg) Set(line string) error {
rd := csv.NewReader(strings.NewReader(line))
rd.FieldsPerRecord = -1
rd.TrimLeadingSpace = true
values, err := rd.Read()
if err == io.EOF {
a.values = nil
return nil
}
if err != nil {
return fmt.Errorf("unable to parse comma-separated argument: %w", err)
}
a.values = values
return nil
}
2021-03-26 22:34:43 +02:00
type CLIArgs struct {
country string
listCountries bool
listProxies bool
bindAddress string
verbosity int
timeout time.Duration
showVersion bool
proxy string
apiLogin string
apiPassword string
apiAddress string
2024-10-06 00:08:17 +03:00
apiClientType string
apiClientVersion string
apiUserAgent string
2024-08-01 12:01:24 +03:00
bootstrapDNS *CSVArg
refresh time.Duration
refreshRetry time.Duration
certChainWorkaround bool
caFile string
2021-03-26 22:34:43 +02:00
}
2024-08-01 12:01:24 +03:00
func parse_args() *CLIArgs {
args := &CLIArgs{
bootstrapDNS: &CSVArg{
values: []string{
"https://1.1.1.3/dns-query",
2024-08-01 12:49:41 +03:00
"https://8.8.8.8/dns-query",
"https://dns.google/dns-query",
2024-08-01 12:01:24 +03:00
"https://security.cloudflare-dns.com/dns-query",
"https://wikimedia-dns.org/dns-query",
"https://dns.adguard-dns.com/dns-query",
"https://dns.quad9.net/dns-query",
"https://doh.cleanbrowsing.org/doh/adult-filter/",
},
},
}
2021-03-26 22:40:27 +02:00
flag.StringVar(&args.country, "country", "EU", "desired proxy location")
2021-03-27 00:43:33 +02:00
flag.BoolVar(&args.listCountries, "list-countries", false, "list available countries and exit")
flag.BoolVar(&args.listProxies, "list-proxies", false, "output proxy list and exit")
flag.StringVar(&args.bindAddress, "bind-address", "127.0.0.1:18080", "HTTP proxy listen address")
2021-03-26 22:34:43 +02:00
flag.IntVar(&args.verbosity, "verbosity", 20, "logging verbosity "+
"(10 - debug, 20 - info, 30 - warning, 40 - error, 50 - critical)")
flag.DurationVar(&args.timeout, "timeout", 10*time.Second, "timeout for network operations")
flag.BoolVar(&args.showVersion, "version", false, "show program version and exit")
2021-03-26 22:40:27 +02:00
flag.StringVar(&args.proxy, "proxy", "", "sets base proxy to use for all dial-outs. "+
"Format: <http|https|socks5|socks5h>://[login:password@]host[:port] "+
2021-03-26 22:34:43 +02:00
"Examples: http://user:password@192.168.1.1:3128, socks5://10.0.0.1:1080")
2024-10-06 00:08:17 +03:00
flag.StringVar(&args.apiClientVersion, "api-client-version", se.DefaultSESettings.ClientVersion, "client version reported to SurfEasy API")
flag.StringVar(&args.apiClientType, "api-client-type", se.DefaultSESettings.ClientType, "client type reported to SurfEasy API")
flag.StringVar(&args.apiUserAgent, "api-user-agent", se.DefaultSESettings.UserAgent, "user agent reported to SurfEasy API")
2021-03-27 00:43:33 +02:00
flag.StringVar(&args.apiLogin, "api-login", "se0316", "SurfEasy API login")
flag.StringVar(&args.apiPassword, "api-password", "SILrMEPBmJuhomxWkfm3JalqHX2Eheg1YhlEZiMh8II", "SurfEasy API password")
2021-03-28 22:21:23 +03:00
flag.StringVar(&args.apiAddress, "api-address", "", fmt.Sprintf("override IP address of %s", API_DOMAIN))
2024-08-01 12:01:24 +03:00
flag.Var(args.bootstrapDNS, "bootstrap-dns",
"comma-separated list of DNS/DoH/DoT/DoQ resolvers for initial discovery of SurfEasy API address. "+
2021-03-30 15:50:57 +03:00
"See https://github.com/ameshkov/dnslookup/ for upstream DNS URL format. "+
2024-08-01 12:01:24 +03:00
"Examples: https://1.1.1.1/dns-query,quic://dns.adguard.com")
flag.DurationVar(&args.refresh, "refresh", 4*time.Hour, "login refresh interval")
2021-04-23 13:01:53 +03:00
flag.DurationVar(&args.refreshRetry, "refresh-retry", 5*time.Second, "login refresh retry interval")
flag.BoolVar(&args.certChainWorkaround, "certchain-workaround", true,
"add bundled cross-signed intermediate cert to certchain to make it check out on old systems")
flag.StringVar(&args.caFile, "cafile", "", "use custom CA certificate bundle file")
2021-03-26 22:34:43 +02:00
flag.Parse()
if args.country == "" {
arg_fail("Country can't be empty string.")
}
2021-03-27 00:43:33 +02:00
if args.listCountries && args.listProxies {
2021-03-26 22:34:43 +02:00
arg_fail("list-countries and list-proxies flags are mutually exclusive")
2021-03-25 20:45:48 +02:00
}
2021-03-26 22:34:43 +02:00
return args
}
2021-03-25 20:45:48 +02:00
2021-03-26 22:34:43 +02:00
func proxyFromURLWrapper(u *url.URL, next xproxy.Dialer) (xproxy.Dialer, error) {
cdialer, ok := next.(ContextDialer)
if !ok {
return nil, errors.New("only context dialers are accepted")
2021-03-25 20:45:48 +02:00
}
2021-03-25 22:09:36 +02:00
2021-03-26 22:34:43 +02:00
return ProxyDialerFromURL(u, cdialer)
}
func run() int {
args := parse_args()
if args.showVersion {
fmt.Println(version)
return 0
2021-03-25 22:09:36 +02:00
}
2021-03-26 21:52:09 +02:00
2021-03-26 22:34:43 +02:00
logWriter := NewLogWriter(os.Stderr)
defer logWriter.Close()
mainLogger := NewCondLogger(log.New(logWriter, "MAIN : ",
log.LstdFlags|log.Lshortfile),
args.verbosity)
proxyLogger := NewCondLogger(log.New(logWriter, "PROXY : ",
log.LstdFlags|log.Lshortfile),
args.verbosity)
2021-03-27 00:43:33 +02:00
mainLogger.Info("opera-proxy client version %s is starting...", version)
2021-03-26 22:34:43 +02:00
var dialer ContextDialer = &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}
2021-03-26 22:34:43 +02:00
if args.proxy != "" {
xproxy.RegisterDialerType("http", proxyFromURLWrapper)
xproxy.RegisterDialerType("https", proxyFromURLWrapper)
proxyURL, err := url.Parse(args.proxy)
if err != nil {
mainLogger.Critical("Unable to parse base proxy URL: %v", err)
return 6
}
pxDialer, err := xproxy.FromURL(proxyURL, dialer)
if err != nil {
mainLogger.Critical("Unable to instantiate base proxy dialer: %v", err)
return 7
}
dialer = pxDialer.(ContextDialer)
}
seclientDialer := dialer
2024-08-01 12:01:24 +03:00
if args.apiAddress != "" || len(args.bootstrapDNS.values) > 0 {
2021-03-28 22:21:23 +03:00
var apiAddress string
if args.apiAddress != "" {
apiAddress = args.apiAddress
mainLogger.Info("Using fixed API host IP address = %s", apiAddress)
} else {
2024-08-01 12:44:23 +03:00
resolver, err := NewResolver(args.bootstrapDNS.values, args.timeout)
2021-03-28 22:21:23 +03:00
if err != nil {
mainLogger.Critical("Unable to instantiate DNS resolver: %v", err)
return 4
}
mainLogger.Info("Discovering API IP address...")
2024-08-01 12:44:23 +03:00
addrs, err := func() ([]netip.Addr, error) {
ctx, cancel := context.WithTimeout(context.Background(), args.timeout)
defer cancel()
2024-08-01 18:22:11 +03:00
defer func() {
resolver = nil
}()
defer resolver.Close()
2024-08-01 12:44:23 +03:00
return resolver.LookupNetIP(ctx, "ip4", API_DOMAIN)
}()
if err != nil {
mainLogger.Critical("Unable to resolve API server address: %v", err)
return 14
}
2021-03-28 22:21:23 +03:00
if len(addrs) == 0 {
mainLogger.Critical("Unable to resolve %s with specified bootstrap DNS", API_DOMAIN)
return 14
}
2024-08-01 12:44:23 +03:00
apiAddress = addrs[0].String()
2021-03-28 22:21:23 +03:00
mainLogger.Info("Discovered address of API host = %s", apiAddress)
}
seclientDialer = NewFixedDialer(apiAddress, dialer)
}
2021-03-27 01:48:24 +02:00
// Dialing w/o SNI, receiving self-signed certificate, so skip verification.
// Either way we'll validate certificate of actual proxy server.
tlsConfig := &tls.Config{
ServerName: "",
2021-03-27 01:48:24 +02:00
InsecureSkipVerify: true,
}
2021-03-27 00:43:33 +02:00
seclient, err := se.NewSEClient(args.apiLogin, args.apiPassword, &http.Transport{
DialContext: seclientDialer.DialContext,
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
conn, err := seclientDialer.DialContext(ctx, network, addr)
2021-03-27 01:48:24 +02:00
if err != nil {
return conn, err
}
return tls.Client(conn, tlsConfig), nil
},
2021-03-27 00:43:33 +02:00
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
})
2024-10-06 00:08:17 +03:00
seclient.Settings.ClientType = args.apiClientType
seclient.Settings.ClientVersion = args.apiClientVersion
seclient.Settings.UserAgent = args.apiUserAgent
2021-03-27 00:43:33 +02:00
if err != nil {
mainLogger.Critical("Unable to construct SEClient: %v", err)
return 8
2021-03-26 22:34:43 +02:00
}
2021-03-27 00:43:33 +02:00
ctx, cl := context.WithTimeout(context.Background(), args.timeout)
err = seclient.AnonRegister(ctx)
if err != nil {
mainLogger.Critical("Unable to perform anonymous registration: %v", err)
return 9
2021-03-26 21:52:09 +02:00
}
2021-03-27 00:43:33 +02:00
cl()
2021-03-26 22:09:37 +02:00
2021-03-27 00:43:33 +02:00
ctx, cl = context.WithTimeout(context.Background(), args.timeout)
err = seclient.RegisterDevice(ctx)
2021-03-26 22:09:37 +02:00
if err != nil {
2021-03-27 00:43:33 +02:00
mainLogger.Critical("Unable to perform device registration: %v", err)
return 10
}
cl()
2021-04-02 01:39:33 +03:00
if args.listCountries {
return printCountries(mainLogger, args.timeout, seclient)
}
ctx, cl = context.WithTimeout(context.Background(), args.timeout)
// TODO: learn about requested_geo value format
ips, err := seclient.Discover(ctx, fmt.Sprintf("\"%s\",,", args.country))
if err != nil {
mainLogger.Critical("Endpoint discovery failed: %v", err)
return 12
}
if args.listProxies {
return printProxies(ips, seclient)
}
if len(ips) == 0 {
mainLogger.Critical("Empty endpoint list!")
return 13
}
2021-04-23 13:01:53 +03:00
runTicker(context.Background(), args.refresh, args.refreshRetry, func(ctx context.Context) error {
mainLogger.Info("Refreshing login...")
2021-04-02 01:15:12 +03:00
reqCtx, cl := context.WithTimeout(ctx, args.timeout)
defer cl()
2021-04-02 01:15:12 +03:00
err := seclient.Login(reqCtx)
if err != nil {
2021-04-02 02:03:16 +03:00
mainLogger.Error("Login refresh failed: %v", err)
2021-04-23 13:01:53 +03:00
return err
}
mainLogger.Info("Login refreshed.")
2021-04-02 01:15:12 +03:00
mainLogger.Info("Refreshing device password...")
reqCtx, cl = context.WithTimeout(ctx, args.timeout)
defer cl()
err = seclient.DeviceGeneratePassword(reqCtx)
if err != nil {
2021-04-02 02:03:16 +03:00
mainLogger.Error("Device password refresh failed: %v", err)
2021-04-23 13:01:53 +03:00
return err
2021-04-02 01:15:12 +03:00
}
mainLogger.Info("Device password refreshed.")
2021-04-23 13:01:53 +03:00
return nil
})
2021-03-27 00:43:33 +02:00
endpoint := ips[0]
auth := func() string {
2021-04-02 01:23:38 +03:00
return basic_auth_header(seclient.GetProxyCredentials())
2021-03-26 22:09:37 +02:00
}
2021-03-26 22:34:43 +02:00
var caPool *x509.CertPool
if args.caFile != "" {
caPool = x509.NewCertPool()
certs, err := ioutil.ReadFile(args.caFile)
if err != nil {
mainLogger.Error("Can't load CA file: %v", err)
return 15
}
if ok := caPool.AppendCertsFromPEM(certs); !ok {
mainLogger.Error("Can't load certificates from CA file")
return 15
}
}
handlerDialer := NewProxyDialer(endpoint.NetAddr(), fmt.Sprintf("%s0.%s", args.country, PROXY_SUFFIX), auth, args.certChainWorkaround, caPool, dialer)
2021-03-27 00:43:33 +02:00
mainLogger.Info("Endpoint: %s", endpoint.NetAddr())
2021-03-26 22:34:43 +02:00
mainLogger.Info("Starting proxy server...")
handler := NewProxyHandler(handlerDialer, proxyLogger)
mainLogger.Info("Init complete.")
2021-03-27 00:43:33 +02:00
err = http.ListenAndServe(args.bindAddress, handler)
2021-03-26 22:34:43 +02:00
mainLogger.Critical("Server terminated with a reason: %v", err)
mainLogger.Info("Shutting down...")
return 0
}
2021-03-27 00:43:33 +02:00
func printCountries(logger *CondLogger, timeout time.Duration, seclient *se.SEClient) int {
ctx, cl := context.WithTimeout(context.Background(), timeout)
defer cl()
list, err := seclient.GeoList(ctx)
if err != nil {
logger.Critical("GeoList error: %v", err)
return 11
}
wr := csv.NewWriter(os.Stdout)
defer wr.Flush()
wr.Write([]string{"country code", "country name"})
for _, country := range list {
wr.Write([]string{country.CountryCode, country.Country})
}
return 0
}
func printProxies(ips []se.SEIPEntry, seclient *se.SEClient) int {
2021-03-27 00:43:33 +02:00
wr := csv.NewWriter(os.Stdout)
defer wr.Flush()
login, password := seclient.GetProxyCredentials()
fmt.Println("Proxy login:", login)
fmt.Println("Proxy password:", password)
fmt.Println("Proxy-Authorization:", basic_auth_header(login, password))
2021-03-27 00:43:33 +02:00
fmt.Println("")
wr.Write([]string{"host", "ip_address", "port"})
for i, ip := range ips {
for _, port := range ip.Ports {
wr.Write([]string{
2021-03-28 22:21:23 +03:00
fmt.Sprintf("%s%d.%s", strings.ToLower(ip.Geo.CountryCode), i, PROXY_SUFFIX),
ip.IP,
fmt.Sprintf("%d", port),
})
2021-03-27 00:43:33 +02:00
}
}
return 0
}
2021-03-26 22:34:43 +02:00
func main() {
os.Exit(run())
2021-03-25 20:45:48 +02:00
}