Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create find-strategy #105

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions x/examples/smart-proxy/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"dns": [
{"system": {}},

{"https": {"name": "2620:fe::fe"}, "//": "Quad9"},
{"https": {"name": "9.9.9.9"}},
{"https": {"name": "149.112.112.112"}},

{"https": {"name": "2001:4860:4860::8888"}, "//": "Google"},
{"https": {"name": "8.8.8.8"}},
{"https": {"name": "2001:4860:4860::8844"}},
{"https": {"name": "8.8.4.4"}},

{"https": {"name": "2606:4700:4700::1111"}, "//": "Cloudflare"},
{"https": {"name": "1.1.1.1"}},
{"https": {"name": "2606:4700:4700::1001"}},
{"https": {"name": "1.0.0.1"}},
{"https": {"name": "cloudflare-dns.com.", "address": "cloudflare.net."}},

{"https": {"name": "2620:119:35::35"}, "//": "OpenDNS"},
{"https": {"name": "208.67.220.220"}},
{"https": {"name": "2620:119:53::53"}},
{"https": {"name": "208.67.222.222"}},

{"https": {"name": "2001:67c:930::1"}, "//": "Wikimedia DNS"},
{"https": {"name": "185.71.138.138"}},

{"https": {"name": "doh.dns.sb", "address": "cloudflare.net:443"}, "//": "DNS.SB"},


{"tls": {"name": "2620:fe::fe"}, "//": "Quad9"},
{"tls": {"name": "9.9.9.9"}},
{"tls": {"name": "149.112.112.112"}},

{"tls": {"name": "2001:4860:4860::8888"}, "//": "Google"},
{"tls": {"name": "8.8.8.8"}},
{"tls": {"name": "2001:4860:4860::8844"}},
{"tls": {"name": "8.8.4.4"}},

{"tls": {"name": "2606:4700:4700::1111"}, "//": "Cloudflare"},
{"tls": {"name": "1.1.1.1"}},
{"tls": {"name": "2606:4700:4700::1001"}},
{"tls": {"name": "1.0.0.1"}},

{"tls": {"name": "2620:119:35::35"}, "//": "OpenDNS"},
{"tls": {"name": "208.67.220.220"}},
{"tls": {"name": "2620:119:53::53"}},
{"tls": {"name": "208.67.222.222"}},

{"tls": {"name": "2001:67c:930::1"}, "//": "Wikimedia DNS"},
{"tls": {"name": "185.71.138.138"}},


{"tcp": {"address": "2620:fe::fe"}, "//": "Quad9"},
{"tcp": {"address": "9.9.9.9"}},
{"tcp": {"address": "149.112.112.112"}},
{"tcp": {"address": "[2620:fe::fe]:9953"}},
{"tcp": {"address": "9.9.9.9:9953"}},
{"tcp": {"address": "149.112.112.112:9953"}},

{"tcp": {"address": "2001:4860:4860::8888"}, "//": "Google"},
{"tcp": {"address": "8.8.8.8"}},
{"tcp": {"address": "2001:4860:4860::8844"}},
{"tcp": {"address": "8.8.4.4"}},

{"tcp": {"address": "2606:4700:4700::1111"}, "//": "Cloudflare"},
{"tcp": {"address": "1.1.1.1"}},
{"tcp": {"address": "2606:4700:4700::1001"}},
{"tcp": {"address": "1.0.0.1"}},

{"tcp": {"address": "2620:119:35::35"}, "//": "OpenDNS"},
{"tcp": {"address": "208.67.220.220"}},
{"tcp": {"address": "2620:119:53::53"}},
{"tcp": {"address": "208.67.222.222"}},
{"tcp": {"address": "[2620:119:35::35]:443"}},
{"tcp": {"address": "208.67.220.220:443"}},
{"tcp": {"address": "[2620:119:35::35]:5353"}},
{"tcp": {"address": "208.67.220.220:5353"}},


{"udp": {"address": "2620:fe::fe"}, "//": "Quad9"},
{"udp": {"address": "9.9.9.9"}},
{"udp": {"address": "149.112.112.112"}},
{"udp": {"address": "[2620:fe::fe]:9953"}},
{"udp": {"address": "9.9.9.9:9953"}},
{"udp": {"address": "149.112.112.112:9953"}},

{"udp": {"address": "2001:4860:4860::8888"}, "//": "Google"},
{"udp": {"address": "8.8.8.8"}},
{"udp": {"address": "2001:4860:4860::8844"}},
{"udp": {"address": "8.8.4.4"}},

{"udp": {"address": "2606:4700:4700::1111"}, "//": "Cloudflare"},
{"udp": {"address": "1.1.1.1"}},
{"udp": {"address": "2606:4700:4700::1001"}},
{"udp": {"address": "1.0.0.1"}},

{"udp": {"address": "2620:119:35::35"}, "//": "OpenDNS"},
{"udp": {"address": "208.67.220.220"}},
{"udp": {"address": "2620:119:53::53"}},
{"udp": {"address": "208.67.222.222"}},
{"udp": {"address": "[2620:119:35::35]:443"}},
{"udp": {"address": "208.67.220.220:443"}},
{"udp": {"address": "[2620:119:35::35]:5353"}},
{"udp": {"address": "208.67.220.220:5353"}}
],

"tls": [
"",
"split:1",
"split:2",
"split:5",
"tlsfrag:1"
]
}
18 changes: 18 additions & 0 deletions x/examples/smart-proxy/config_broken.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"dns": [
{"udp": {"address": "china.cn"}},
{"udp": {"address": "ns1.tic.ir"}},
{"tcp": {"address": "ns1.tic.ir"}},
{"udp": {"address": "tmcell.tm"}},
{"tls": {"name": "captive-portal.badssl.com", "address": "captive-portal.badssl.com:443"}},
{"https": {"name": "mitm-software.badssl.com"}}
],

"tls": [
"",
"split:1",
"split:2",
"split:5",
"tlsfrag:1"
]
}
145 changes: 145 additions & 0 deletions x/examples/smart-proxy/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// Copyright 2023 Jigsaw Operations LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"os/signal"
"strings"
"time"

"github.com/Jigsaw-Code/outline-sdk/transport"
"github.com/Jigsaw-Code/outline-sdk/x/config"
"github.com/Jigsaw-Code/outline-sdk/x/httpproxy"
"github.com/Jigsaw-Code/outline-sdk/x/smart"
)

var debugLog log.Logger = *log.New(io.Discard, "", 0)

type stringArrayFlagValue []string

func (v *stringArrayFlagValue) String() string {
return fmt.Sprint(*v)
}

func (v *stringArrayFlagValue) Set(value string) error {
*v = append(*v, value)
return nil
}

func main() {
verboseFlag := flag.Bool("v", false, "Enable debug output")
addrFlag := flag.String("localAddr", "localhost:1080", "Local proxy address")
configFlag := flag.String("config", "config.json", "Address of the config file")
transportFlag := flag.String("transport", "", "The base transport for the connections")
var domainsFlag stringArrayFlagValue
flag.Var(&domainsFlag, "domain", "The test domains to find strategies.")

flag.Parse()
if *verboseFlag {
debugLog = *log.New(os.Stderr, "", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)
}

if len(domainsFlag) == 0 {
log.Fatal("Must specify flag --domain")
}

if *configFlag == "" {
log.Fatal("Must specify flag --config")
}

finderConfig, err := os.ReadFile(*configFlag)
if err != nil {
log.Fatalf("Could not read config: %v", err)
}

packetDialer, err := config.NewPacketDialer(*transportFlag)
if err != nil {
log.Fatalf("Could not create packet dialer: %v", err)
}
streamDialer, err := config.NewStreamDialer(*transportFlag)
if err != nil {
log.Fatalf("Could not create stream dialer: %v", err)
}
if strings.HasPrefix(*transportFlag, "ss:") {
innerDialer := streamDialer
// Hack to disable IPv6 with Shadowsocks, since it doesn't communicate connection success.
streamDialer = transport.FuncStreamDialer(func(ctx context.Context, addr string) (transport.StreamConn, error) {
host, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
if ip := net.ParseIP(host); ip != nil && ip.To4() == nil {
return nil, fmt.Errorf("IPv6 not supported")
}
return innerDialer.DialStream(ctx, addr)
})
}
finder := smart.StrategyFinder{
LogWriter: debugLog.Writer(),
TestTimeout: 5 * time.Second,
StreamDialer: streamDialer,
PacketDialer: packetDialer,
}

fmt.Println("Finding strategy")
dialer, err := finder.NewDialer(context.Background(), domainsFlag, finderConfig)
if err != nil {
log.Fatalf("Failed to find dialer: %v", err)
}
logDialer := transport.FuncStreamDialer(func(ctx context.Context, address string) (transport.StreamConn, error) {
conn, err := dialer.DialStream(ctx, address)
if err != nil {
debugLog.Printf("Failed to dial %v: %v\n", address, err)
}
return conn, err
})

listener, err := net.Listen("tcp", *addrFlag)
if err != nil {
log.Fatalf("Could not listen on address %v: %v", *addrFlag, err)
}
defer listener.Close()
fmt.Printf("Proxy listening on %v\n", listener.Addr().String())

server := http.Server{
Handler: httpproxy.NewProxyHandler(logDialer),
ErrorLog: &debugLog,
}
go func() {
if err := server.Serve(listener); err != nil && err != http.ErrServerClosed {
log.Fatalf("Error running web server: %v", err)
}
}()

// Wait for interrupt signal to stop the proxy.
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
<-sig
fmt.Print("Shutting down")
// Gracefully shut down the server, with a 5s timeout.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("Failed to shutdown gracefully: %v", err)
}
}
25 changes: 21 additions & 4 deletions x/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,41 @@ module github.com/Jigsaw-Code/outline-sdk/x
go 1.20

require (
github.com/Jigsaw-Code/outline-sdk v0.0.12-0.20240117212550-6cd87709dc1e
github.com/Jigsaw-Code/outline-sdk v0.0.13-0.20240212211908-de32e39b12e5
github.com/go-task/task/v3 v3.34.1
github.com/songgao/water v0.0.0-20190725173103-fd331bda3f4b
github.com/stretchr/testify v1.8.2
github.com/stretchr/testify v1.8.4
github.com/vishvananda/netlink v1.1.0
golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a
golang.org/x/net v0.19.0
golang.org/x/sys v0.15.0
golang.org/x/sys v0.16.0
)

require (
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/eycorsican/go-tun2socks v1.16.11 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-zglob v0.0.4 // indirect
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/radovskyb/watcher v1.0.7 // indirect
github.com/sajari/fuzzy v1.0.0 // indirect
github.com/shadowsocks/go-shadowsocks2 v0.1.5 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20240110193028-0dcbfd608b1e // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/tools v0.16.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
mvdan.cc/sh/v3 v3.7.0 // indirect
)
Loading
Loading