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

Merge iovxw's work #1

Draft
wants to merge 11 commits into
base: dev
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
59 changes: 0 additions & 59 deletions README.md

This file was deleted.

11 changes: 9 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
module NekoXProxy

go 1.16
go 1.18

require (
github.com/gorilla/websocket v1.4.2
github.com/asergeyev/nradix v0.0.0-20170505151046-3872ab85bb56
nhooyr.io/websocket v1.8.7
tailscale.com v1.34.2
)

require (
github.com/klauspost/compress v1.15.4 // indirect
golang.org/x/sys v0.3.1-0.20221220025402-2204b6615fb8 // indirect
)
74 changes: 73 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1 +1,73 @@
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/asergeyev/nradix v0.0.0-20170505151046-3872ab85bb56 h1:Wi5Tgn8K+jDcBYL+dIMS1+qXYH2r7tpRAyBgqrWfQtw=
github.com/asergeyev/nradix v0.0.0-20170505151046-3872ab85bb56/go.mod h1:8BhOLuqtSuT5NZtZMwfvEibi09RO3u79uqfHZzfDTR4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.14.0 h1:+cqqvzZV87b4adx/5ayVOaYZ2CrvM4ejQvUdBzPPUss=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0=
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8=
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
github.com/gobwas/ws v1.0.2 h1:CoAavW/wd/kulfZmSIBt6p24n4j7tHgNVCjsfHVNUbo=
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/compress v1.15.4 h1:1kn4/7MepF/CHmYub99/nNX8az0IJjfSOU/jbnTVfqQ=
github.com/klauspost/compress v1.15.4/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.8.1-0.20211023094830-115ce09fd6b4 h1:Ha8xCaq6ln1a+R91Km45Oq6lPXj2Mla6CRJYcuV2h1w=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
go4.org/mem v0.0.0-20210711025021-927187094b94 h1:OAAkygi2Js191AJP1Ds42MhJRgeofeKGjuoUqNp1QC4=
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f h1:OeJjE6G4dgCY4PIXvIRQbE8+RX+uXZyGhUy/ksMGJoc=
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.3.1-0.20221220025402-2204b6615fb8 h1:/VqMvhQCyzfuc826eNrpWmMb3AwD2Sxz/HMsYIhwcIs=
golang.org/x/sys v0.3.1-0.20221220025402-2204b6615fb8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
tailscale.com v1.34.2 h1:g6n9HEZjQfTQfXyeiGeuxPPqSQKhtdmXIbyUxd76O9U=
tailscale.com v1.34.2/go.mod h1:xA1XYcaHK0BIyCETEkTuaj3M3uOe/zNJ5G/Sb9oNU3k=
153 changes: 128 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,149 @@
package main

import (
"context"
"encoding/base64"
"flag"
"fmt"
"log"
"net/http"
"math"
"net"
"net/url"
"os"
"strings"
"time"
)

var nekoXProxyString string

var nekoXProxyBaseDomain string
var nekoXProxyDomains []string

var client *http.Client
"github.com/asergeyev/nradix"
"nhooyr.io/websocket"
)

func main() {
listen := flag.String("l", "127.0.0.1:26641", "HttpProxy listen port")
_nekoXProxyString := flag.String("p", "", "NekoX Proxy URL (keep empty if you don't know)")
listen := flag.String("l", "127.0.0.1:26641", "listen address")
flag.Parse()

var ok bool
if *_nekoXProxyString != "" {
ok = parseNekoXString(base64.RawURLEncoding.EncodeToString([]byte(*_nekoXProxyString)))
} else {
log.Println("Getting NekoX public proxy...")
ok = parseNekoXString(getNekoXString())
url := flag.Arg(0)
if url == "" {
fmt.Fprintln(os.Stderr, "NekoX Proxy URL is required")
return
}

if !ok {
log.Println("Failed to parse NekoX proxy.")
router, err := NewRouter(url)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}

client = &http.Client{}
dialer := func(ctx context.Context, network, addr string) (net.Conn, error) {
if network != "tcp" {
return nil, fmt.Errorf("tcp only: %s", network)
}
wsurl, found := router.IP2URL(addr)
if !found {
return nil, fmt.Errorf("New DC address found: %s", addr)
}

dial_ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
c, _, err := websocket.Dial(dial_ctx, wsurl,
&websocket.DialOptions{
Subprotocols: []string{"binary"},
// disable compression for faster media download
CompressionMode: websocket.CompressionDisabled,
})
if err != nil {
return nil, err
}
c.SetReadLimit(math.MaxInt64 - 1) // disable the buggy limitReader
return websocket.NetConn(ctx, c, websocket.MessageBinary), nil
}

server := Server{Dialer: dialer}

l, err := net.Listen("tcp", *listen)
if err != nil {
log.Fatal(err)
}
defer l.Close()
log.Println("Telegram Socks5 Proxy started at", *listen)
log.Fatal(server.Serve(l))
}

type Router struct {
baseDomain string
subDomainMapper []string
dcMapper *nradix.Tree
}

http.HandleFunc("/", relay)
server := &http.Server{
Addr: *listen,
WriteTimeout: 10 * time.Second,
func NewRouter(nekoXURL string) (*Router, error) {
url, err := url.ParseRequestURI(nekoXURL)
if err != nil {
return nil, err
}

pldstr, _ := base64.RawURLEncoding.DecodeString(url.Query().Get("payload"))

router := Router{
baseDomain: url.Host,
subDomainMapper: strings.Split(string(pldstr), ","),
dcMapper: nradix.NewTree(0),
}

router.initDcMapper()
return &router, nil
}

func (router *Router) initDcMapper() {
router.dcMapper.AddCIDR("149.154.175.5", 1)
router.dcMapper.AddCIDR("95.161.76.100", 2)
router.dcMapper.AddCIDR("149.154.175.100", 3)
router.dcMapper.AddCIDR("149.154.167.91", 4)
router.dcMapper.AddCIDR("149.154.167.92", 4)
router.dcMapper.AddCIDR("149.154.171.5", 5)
router.dcMapper.AddCIDR("2001:b28:f23d:f001::a", 1)
router.dcMapper.AddCIDR("2001:67c:4e8:f002::a", 2)
router.dcMapper.AddCIDR("2001:b28:f23d:f003::a", 3)
router.dcMapper.AddCIDR("2001:67c:4e8:f004::a", 4)
router.dcMapper.AddCIDR("2001:b28:f23f:f005::a", 5)
router.dcMapper.AddCIDR("149.154.161.144", 2)
router.dcMapper.AddCIDR("149.154.167.0/24", 2)
router.dcMapper.AddCIDR("149.154.175.1", 3)
router.dcMapper.AddCIDR("91.108.4.0/24", 4)
router.dcMapper.AddCIDR("149.154.164.0/24", 4)
router.dcMapper.AddCIDR("149.154.165.0/24", 4)
router.dcMapper.AddCIDR("149.154.166.0/24", 4)
router.dcMapper.AddCIDR("91.108.56.0/24", 5)
router.dcMapper.AddCIDR("2001:b28:f23d:f001::d", 1)
router.dcMapper.AddCIDR("2001:67c:4e8:f002::d", 2)
router.dcMapper.AddCIDR("2001:b28:f23d:f003::d", 3)
router.dcMapper.AddCIDR("2001:67c:4e8:f004::d", 4)
router.dcMapper.AddCIDR("2001:b28:f23f:f005::d", 5)
router.dcMapper.AddCIDR("149.154.175.40", 6)
router.dcMapper.AddCIDR("149.154.167.40", 7)
router.dcMapper.AddCIDR("149.154.175.117", 8)
router.dcMapper.AddCIDR("2001:b28:f23d:f001::e", 6)
router.dcMapper.AddCIDR("2001:67c:4e8:f002::e", 7)
router.dcMapper.AddCIDR("2001:b28:f23d:f003::e", 8)

router.dcMapper.AddCIDR("2001:b28:f23d:f001::b", 1)
router.dcMapper.AddCIDR("2001:67c:4e8:f002::b", 2)
router.dcMapper.AddCIDR("2001:b28:f23d:f003::b", 3)
router.dcMapper.AddCIDR("2001:67c:4e8:f004::b", 4)
router.dcMapper.AddCIDR("2001:b28:f23f:f005::b", 5)

router.dcMapper.AddCIDR("149.154.175.55", 1)
}

func (router *Router) IP2URL(addr string) (string, bool) {
ip, _, err := net.SplitHostPort(addr)
if err != nil {
panic(err.Error())
}
dc, err := router.dcMapper.FindCIDR(ip)
if dc == nil || err != nil {
return "", false
}
url := fmt.Sprintf("wss://%s.%s/api", router.subDomainMapper[dc.(int)-1], router.baseDomain)
log.Printf("new connection to DC%d (%s)\n", dc, addr)

log.Println("Telegram HTTP Proxy started at", *listen)
server.ListenAndServe()
return url, true
}
40 changes: 0 additions & 40 deletions relay.go

This file was deleted.

Loading