github.com/geph-official/geph2@v0.22.6-0.20210211030601-f527cb59b0df/cmd/geph-exit/main.go (about) 1 package main 2 3 import ( 4 "crypto/ed25519" 5 "flag" 6 "io/ioutil" 7 "net" 8 "net/http" 9 "os" 10 "time" 11 12 _ "net/http/pprof" 13 14 statsd "github.com/etsy/statsd/examples/go" 15 "github.com/geph-official/geph2/libs/bdclient" 16 "github.com/geph-official/geph2/libs/fastudp" 17 "github.com/geph-official/geph2/libs/kcp-go" 18 "github.com/geph-official/geph2/libs/niaucchi4" 19 "github.com/geph-official/geph2/libs/pseudotcp" 20 "github.com/patrickmn/go-cache" 21 log "github.com/sirupsen/logrus" 22 "golang.org/x/time/rate" 23 ) 24 25 var keyfile string 26 var pubkey ed25519.PublicKey 27 var seckey ed25519.PrivateKey 28 var onlyPaid bool 29 30 var singleHop string 31 32 var binderFront string 33 var binderReal string 34 var bclient *bdclient.Client 35 var hostname string 36 var statsdAddr string 37 38 var infiniteLimit = rate.NewLimiter(rate.Inf, 1000) 39 var listenHost string 40 41 var statClient *statsd.StatsdClient 42 43 var ipcache = cache.New(time.Hour, time.Hour) 44 45 func main() { 46 var err error 47 48 log.SetFormatter(&log.TextFormatter{ 49 FullTimestamp: false, 50 }) 51 log.SetLevel(log.DebugLevel) 52 flag.StringVar(&keyfile, "keyfile", "keyfile.bin", "location of key file") 53 flag.StringVar(&binderFront, "binderFront", "https://binder.geph.io/v2", "binder domain-fronting host") 54 flag.StringVar(&binderReal, "binderReal", "binder.geph.io", "real hostname of the binder") 55 flag.StringVar(&statsdAddr, "statsdAddr", "c2.geph.io:8125", "address of StatsD for gathering statistics") 56 flag.BoolVar(&onlyPaid, "onlyPaid", false, "only allow paying users") 57 flag.StringVar(&singleHop, "singleHop", "", "if supplied, runs in single-hop mode. (for example, -singleHop :5000 would listen on port 5000)") 58 flag.StringVar(&listenHost, "listenHost", "", "specify the specific host to listen on") 59 flag.StringVar(&hostname, "hostname", "", "force the use of a particular hostname") 60 flag.Parse() 61 go func() { 62 log.Println(http.ListenAndServe("localhost:6060", nil)) 63 }() 64 65 if hostname == "" { 66 hostname, err = os.Hostname() 67 if err != nil { 68 statsdAddr = "" 69 } else { 70 } 71 } 72 73 // load the key 74 kcp.CongestionControl = "BIC" 75 loadKey() 76 if singleHop != "" { 77 mainSingleHop() 78 } 79 log.Infof("Loaded PK = %x", pubkey) 80 81 if statsdAddr != "" { 82 z, e := net.ResolveUDPAddr("udp", statsdAddr) 83 if e != nil { 84 panic(e) 85 } 86 statClient = statsd.New(z.IP.String(), z.Port) 87 } 88 bclient = bdclient.NewClient(binderFront, binderReal, "geph_exit") 89 90 // listen 91 go func() { 92 tcpListener, err := net.Listen("tcp", listenHost+":2389") 93 if err != nil { 94 panic(err) 95 } 96 log.Infof("Listen on TCP 2389") 97 for { 98 rawClient, err := tcpListener.Accept() 99 if err != nil { 100 log.Println(err) 101 continue 102 } 103 rawClient.(*net.TCPConn).SetKeepAlive(false) 104 go handle(rawClient) 105 } 106 }() 107 go func() { 108 tcpListener, err := pseudotcp.Listen(listenHost + ":12389") 109 if err != nil { 110 panic(err) 111 } 112 log.Infof("Listen on PTCP 12389") 113 for { 114 rawClient, err := tcpListener.Accept() 115 if err != nil { 116 log.Println(err) 117 continue 118 } 119 go handle(rawClient) 120 } 121 }() 122 go e2elisten() 123 udpsock, err := net.ListenPacket("udp4", listenHost+":2389") 124 if err != nil { 125 panic(err) 126 } 127 udpsock.(*net.UDPConn).SetWriteBuffer(100 * 1024 * 1024) 128 udpsock.(*net.UDPConn).SetReadBuffer(100 * 1024 * 1024) 129 obfs := niaucchi4.ObfsListen(make([]byte, 32), udpsock, false) 130 kcpListener := niaucchi4.ListenKCP(obfs) 131 log.Infoln("Listen on UDP 2389") 132 for { 133 rc, err := kcpListener.Accept() 134 if err != nil { 135 continue 136 } 137 rc.SetWindowSize(10000, 10000) 138 rc.SetNoDelay(0, 100, 32, 0) 139 rc.SetStreamMode(true) 140 rc.SetMtu(1300) 141 go handle(rc) 142 } 143 } 144 145 func e2elisten() { 146 udpsock, err := net.ListenPacket("udp4", listenHost+":2399") 147 if err != nil { 148 panic(err) 149 } 150 fastsock := fastudp.NewConn(udpsock.(*net.UDPConn)) 151 udpsock.(*net.UDPConn).SetWriteBuffer(100 * 1024 * 1024) 152 udpsock.(*net.UDPConn).SetReadBuffer(100 * 1024 * 1024) 153 log.Infoln("e2elisten on UDP 2399") 154 e2e := niaucchi4.NewE2EConn(fastsock) 155 kcpListener := niaucchi4.ListenKCP(e2e) 156 for { 157 rc, err := kcpListener.Accept() 158 if err != nil { 159 log.Println("error while accepting E2E:", err) 160 continue 161 } 162 rc.SetWindowSize(10000, 10000) 163 rc.SetNoDelay(0, 100, 32, 0) 164 rc.SetStreamMode(true) 165 rc.SetMtu(1300) 166 go handle(rc) 167 } 168 } 169 170 func loadKey() { 171 retry: 172 bts, err := ioutil.ReadFile(keyfile) 173 if err != nil { 174 // genkey 175 _, key, _ := ed25519.GenerateKey(nil) 176 ioutil.WriteFile(keyfile, key, 0600) 177 goto retry 178 } 179 seckey = bts 180 pubkey = seckey.Public().(ed25519.PublicKey) 181 }