|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "log" |
| 9 | + "main/common" |
| 10 | + "net" |
| 11 | + "net/url" |
| 12 | + "os" |
| 13 | + "strconv" |
| 14 | + |
| 15 | + "github.com/gorilla/websocket" |
| 16 | + uuid "github.com/satori/go.uuid" |
| 17 | +) |
| 18 | + |
| 19 | +var wsServer *string |
| 20 | + |
| 21 | +func main() { |
| 22 | + os.Setenv("http_proxy", "") |
| 23 | + os.Setenv("https_proxy", "") |
| 24 | + os.Setenv("HTTP_PROXY", "") |
| 25 | + os.Setenv("HTTPS_PROXY", "") |
| 26 | + os.Setenv("ALL_PROXY", "") |
| 27 | + |
| 28 | + listenAddr := flag.String("listen", ":1180", "listen socks5 address") |
| 29 | + wsServer = flag.String("ws", "localhost:80", "websocket server address") |
| 30 | + flag.Parse() |
| 31 | + log.SetFlags(log.LstdFlags | log.Lshortfile | log.Lmicroseconds) |
| 32 | + listener, err := net.Listen("tcp", *listenAddr) |
| 33 | + if err != nil { |
| 34 | + log.Fatalf("Failed to listen on %s: %v", listenAddr, err) |
| 35 | + } |
| 36 | + |
| 37 | + log.Printf("SOCKS5 proxy server is listening on %s", *listenAddr) |
| 38 | + |
| 39 | + for { |
| 40 | + conn, err := listener.Accept() |
| 41 | + if err != nil { |
| 42 | + log.Printf("Failed to accept connection: %v", err) |
| 43 | + continue |
| 44 | + } |
| 45 | + |
| 46 | + go handleConnection(conn) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func handleConnection(clientConn net.Conn) { |
| 51 | + defer clientConn.Close() |
| 52 | + |
| 53 | + // Step 1: Version identification and authentication |
| 54 | + // Read and verify the SOCKS5 initial handshake message |
| 55 | + buf := make([]byte, 257) |
| 56 | + _, err := io.ReadAtLeast(clientConn, buf, 2) |
| 57 | + if err != nil { |
| 58 | + log.Printf("Failed to read client handshake: %v", err) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + // Check SOCKS version and authentication methods |
| 63 | + if buf[0] != 0x05 { |
| 64 | + log.Printf("Unsupported SOCKS version: %v", buf[0]) |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + // Number of authentication methods supported |
| 69 | + numMethods := int(buf[1]) |
| 70 | + authMethods := buf[2 : 2+numMethods] |
| 71 | + |
| 72 | + // Check if "no authentication" method (0x00) is supported |
| 73 | + noAuth := false |
| 74 | + for _, m := range authMethods { |
| 75 | + if m == 0x00 { |
| 76 | + noAuth = true |
| 77 | + break |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if !noAuth { |
| 82 | + log.Printf("No supported authentication methods") |
| 83 | + // Send handshake failure response to client |
| 84 | + clientConn.Write([]byte{0x05, 0xFF}) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + // Send handshake response to client indicating "no authentication" method |
| 89 | + clientConn.Write([]byte{0x05, 0x00}) |
| 90 | + |
| 91 | + // Step 2: Request processing |
| 92 | + // Read and verify the SOCKS5 request |
| 93 | + _, err = io.ReadAtLeast(clientConn, buf, 4) |
| 94 | + if err != nil { |
| 95 | + log.Printf("Failed to read client request: %v", err) |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + if buf[0] != 0x05 { |
| 100 | + log.Printf("Unsupported SOCKS version: %v", buf[0]) |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + if buf[1] != 0x01 { |
| 105 | + log.Printf("Unsupported command: %v", buf[1]) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + // Check the address type |
| 110 | + var destAddr string |
| 111 | + var destPort string |
| 112 | + switch buf[3] { |
| 113 | + case 0x01: // IPv4 address |
| 114 | + ip := net.IP(buf[4 : 4+net.IPv4len]) |
| 115 | + destAddr = ip.String() |
| 116 | + destPort = fmt.Sprintf("%d", int(buf[8])<<8+int(buf[9])) |
| 117 | + case 0x03: // Domain name |
| 118 | + domainLen := int(buf[4]) |
| 119 | + domain := string(buf[5 : 5+domainLen]) |
| 120 | + destAddr = domain |
| 121 | + destPort = strconv.Itoa(int(buf[5+domainLen])<<8 + int(buf[5+domainLen+1])) |
| 122 | + case 0x04: // IPv6 address |
| 123 | + ip := net.IP(buf[4 : 4+net.IPv6len]) |
| 124 | + destAddr = ip.String() |
| 125 | + destPort = strconv.Itoa(int(buf[20])<<8 + int(buf[21])) |
| 126 | + default: |
| 127 | + log.Printf("Unsupported address type: %v", buf[3]) |
| 128 | + return |
| 129 | + } |
| 130 | + |
| 131 | + // Send request response to client indicating success |
| 132 | + clientConn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}) |
| 133 | + |
| 134 | + u := url.URL{ |
| 135 | + Scheme: "ws", |
| 136 | + Host: *wsServer, |
| 137 | + Path: "/chat", |
| 138 | + } |
| 139 | + msgId := uuid.NewV4().String() |
| 140 | + log.Printf("[%s]Client requests proxy connection: %s:%s", msgId, destAddr, destPort) |
| 141 | + // connection to websocket server |
| 142 | + conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil) |
| 143 | + if err != nil { |
| 144 | + log.Printf("[%s]Failed to connect to the WebSocket server: %v", msgId, err) |
| 145 | + clientConn.Close() |
| 146 | + return |
| 147 | + } |
| 148 | + ws := common.WsConn{ |
| 149 | + Conn: conn, |
| 150 | + } |
| 151 | + ws.Lock() |
| 152 | + err = ws.Conn.WriteJSON(common.Proto{ |
| 153 | + MsgType: common.ReqConnect, |
| 154 | + Data: []byte(base64.StdEncoding.EncodeToString([]byte(destAddr + ":" + destPort))), |
| 155 | + MsgId: msgId, |
| 156 | + }) |
| 157 | + ws.Unlock() |
| 158 | + if err != nil { |
| 159 | + log.Printf("[%s]Failed to write connect request to WebSocket server: %v", msgId, err) |
| 160 | + clientConn.Close() |
| 161 | + return |
| 162 | + } |
| 163 | + connectResp := common.Proto{} |
| 164 | + |
| 165 | + err = ws.Conn.ReadJSON(&connectResp) |
| 166 | + if err != nil { |
| 167 | + log.Printf("[%s]Failed to request proxy target from WebSocket server: %v", msgId, err) |
| 168 | + clientConn.Close() |
| 169 | + return |
| 170 | + } |
| 171 | + |
| 172 | + if connectResp.MsgType != common.ReqConnect { |
| 173 | + log.Printf("[%s]ReqConnect failed: %v", msgId, err) |
| 174 | + ws.Conn.Close() |
| 175 | + clientConn.Close() |
| 176 | + return |
| 177 | + } |
| 178 | + go func() { |
| 179 | + resp := common.Proto{} |
| 180 | + for { |
| 181 | + err = ws.Conn.ReadJSON(&resp) |
| 182 | + if err != nil { |
| 183 | + log.Printf("[%s]Failed to read data from WebSocket server: %v", msgId, err) |
| 184 | + break |
| 185 | + } |
| 186 | + _, err = clientConn.Write(resp.Data) |
| 187 | + if err != nil { |
| 188 | + log.Printf("[%s]Failed to write data to the client: %v", msgId, err) |
| 189 | + break |
| 190 | + } |
| 191 | + } |
| 192 | + }() |
| 193 | + for { |
| 194 | + var buf [10240]byte |
| 195 | + n, err := clientConn.Read(buf[:]) |
| 196 | + if err != nil { |
| 197 | + log.Printf("[%s]Failed to read data from the client: %v", msgId, err) |
| 198 | + break |
| 199 | + } |
| 200 | + ws.Lock() |
| 201 | + err = ws.Conn.WriteJSON(common.Proto{ |
| 202 | + MsgType: common.ReqData, |
| 203 | + Data: buf[:n], |
| 204 | + }) |
| 205 | + ws.Unlock() |
| 206 | + if err != nil { |
| 207 | + log.Printf("[%s]Failed to write data to the WebSocket server: %v", msgId, err) |
| 208 | + break |
| 209 | + } |
| 210 | + } |
| 211 | + ws.Conn.Close() |
| 212 | + clientConn.Close() |
| 213 | + log.Printf("[%s]Closing the connection", msgId) |
| 214 | +} |
0 commit comments