{"id":13481781,"url":"https://github.com/haochen233/socks5","last_synced_at":"2025-03-27T12:31:30.073Z","repository":{"id":41581812,"uuid":"376267174","full_name":"haochen233/socks5","owner":"haochen233","description":"A Go library about socks5, supports all socks5 commands. That Provides server and client and easy to use. Compatible with socks4 and socks4a.","archived":false,"fork":false,"pushed_at":"2021-11-26T07:16:58.000Z","size":77,"stargazers_count":47,"open_issues_count":0,"forks_count":10,"subscribers_count":5,"default_branch":"main","last_synced_at":"2024-10-30T15:51:05.270Z","etag":null,"topics":["full-featured","go","golang","socks","socks-client","socks-server","socks4","socks4a","socks5"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/haochen233.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-06-12T10:56:05.000Z","updated_at":"2024-09-20T20:52:57.000Z","dependencies_parsed_at":"2022-09-26T20:00:38.171Z","dependency_job_id":null,"html_url":"https://github.com/haochen233/socks5","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haochen233%2Fsocks5","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haochen233%2Fsocks5/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haochen233%2Fsocks5/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haochen233%2Fsocks5/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/haochen233","download_url":"https://codeload.github.com/haochen233/socks5/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245844981,"owners_count":20681810,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["full-featured","go","golang","socks","socks-client","socks-server","socks4","socks4a","socks5"],"created_at":"2024-07-31T17:00:55.583Z","updated_at":"2025-03-27T12:31:29.607Z","avatar_url":"https://github.com/haochen233.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# socks5\n\nThis is a Golang implementation of the Socks5 protocol library.   \nTo see in this [SOCKS Protocol Version 5](https://www.rfc-editor.org/rfc/rfc1928.html).  \nThis library is also compatible with Socks4 and Socks4a.\n\n# Contents\n\n- [Features](#Features)\n- [Install](#Installation)\n- [Examples](#Examples)\n    - [Server example](#Server-example)\n        - [simple (no authentication)](#simple-no-authentication)\n        - [username/password authentication in memory](#username/password-authentication-in-memory)\n        - [custom transporter to transmit data between client and remote](#custom-transporter-to-transmit-data-between-client-and-remote)\n    - [Client example](#Client)\n        - [CONNECT usage](#CONNECT-usage)\n        - [UDP_ASSOCIATE usage](#UDP_ASSOCIATE-usage)\n        - [BIND usage](#BIND-usage)\n- [FAQ](#FAQ)\n\n# Features\n\n- socks5:\n    - command: **CONNECT**, **UDP ASSOCIATE**, **BIND**.\n    - auth methods:\n        - **Username/Password** authentication.\n        - No Authentication Required.\n- socks4:\n    - command: **CONNECT**, **BIND**.\n    - auth: (no support).\n- sock4a: same as socks4.\n- Custom client and server authenticator.\n- Easy to read source code.\n- Similar to the Golang standard library experience.\n\n# Installation\n\n``` sh\n$ go get \"github.com/haochen233/socks5\"`\n```\n\n# Examples\n\n## Server example\n\n### simple (no authentication):\n\n```go\npackage main\n\nimport (\n\t\"log\"\n\t\"github.com/haochen233/socks5\"\n)\n\nfunc main() {\n\t// create socks server.\n\tsrv := \u0026socks5.Server{\n\t\t// socks server listen address.\n\t\tAddr: \"127.0.0.1:1080\",\n\t\t// UDP assocaite and bind command listen ip.\n\t\t// Don't need port, the port will automatically chosen.\n\t\tBindIP: \"127.0.0.1\",\n\t\t// if nil server will provide no authentication required method.\n\t\tAuthenticators: nil,\n\t}\n\n\t// start listen\n\terr := srv.ListenAndServe()\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n\n\n```\n\n### username/password authentication in memory:\n\n```go\npackage main\n\nimport (\n\t\"crypto/md5\"\n\t\"log\"\n\n\t\"github.com/haochen233/socks5\"\n)\n\nfunc main() {\n\t// create a username/password store in memory.\n\tvar userStorage socks5.UserPwdStore = socks5.NewMemeryStore(md5.New(), \"secret\")\n\t// set a pair of username/password.\n\tuserStorage.Set(\"admin\", \"123456\")\n\n\tsrv := \u0026socks5.Server{\n\t\tAddr:   \"127.0.0.1:1080\",\n\t\tBindIP: \"127.0.0.1\",\n\t\t// enable username/password method and authenticator.\n\t\tAuthenticators: map[socks5.METHOD]socks5.Authenticator{\n\t\t\tsocks5.USERNAME_PASSWORD: socks5.UserPwdAuth{UserPwdStore: userStorage},\n\t\t\t// There is already an authentication method.\n\t\t\t// If want enable no authentication required method.\n\t\t\t// you should enable it explicit.\n\t\t\tsocks5.NO_AUTHENTICATION_REQUIRED: socks5.NoAuth{},\n\t\t},\n\t}\n\n\terr := srv.ListenAndServe()\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n```\n\n### custom transporter to transmit data between client and remote.\n\n```go\npackage main\n\nimport (\n\t\"log\"\n\t\"net\"\n\n\t\"github.com/haochen233/socks5\"\n)\n\n// simulate to impl socks5.Transporter interface.\n// transport encrypted data.\ntype cryptTransport struct {\n}\n\nfunc (c *cryptTransport) TransportTCP(client *net.TCPConn, remote *net.TCPConn) \u003c-chan error {\n\t//encrypt data and send to remote\n\t//decrypt data and send to client\n\treturn nil\n}\n\nfunc (c *cryptTransport) TransportUDP(server *socks5.UDPConn, request *socks5.Request) error {\n\tpanic(\"implement me\")\n\treturn nil\n}\n\nfunc main() {\n\tserver := \u0026socks5.Server{\n\t\tAddr:   \"127.0.0.1:1080\",\n\t\tBindIP: \"127.0.0.1\",\n\t\t// replace default Transporter interface\n\t\tTransporter: \u0026cryptTransport{},\n\t}\n\n\terr := server.ListenAndServe()\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n```\n\n## Client example\n\n### CONNECT usage:\n\n```go\npackage main\n\nimport (\n\t\"log\"\n\n\t\"github.com/haochen233/socks5\"\n)\n\nfunc main() {\n\t// create socks client\n\tclnt := socks5.Client{\n\t\tProxyAddr: \"127.0.0.1:1080\",\n\t\t// Authenticator supported by the client.\n\t\t// It must not be nil.\n\t\tAuth: map[socks5.METHOD]socks5.Authenticator{\n\t\t\t// If client want send NO_AUTHENTICATION_REQUIRED method to server, must\n\t\t\t// add socks5.NoAuth authenticator explicitly\n\t\t\tsocks5.NO_AUTHENTICATION_REQUIRED: \u0026socks5.NoAuth{},\n\t\t},\n\t}\n\n\t// client send CONNECT command and get a tcp connection.\n\t// and use this connection transit data between you and www.google.com:80.\n\tconn, err := clnt.Connect(socks5.Version5, \"www.baidu.com:80\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// close connection.\n\tconn.Close()\n}\n\n```\n\n### UDP_ASSOCIATE usage:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\n\t\"github.com/haochen233/socks5\"\n)\n\nfunc main() {\n\tclnt := socks5.Client{\n\t\tProxyAddr: \"127.0.0.1:1080\",\n\t\t// client provide USERNAME_PASSWORD method and \n\t\t// NO_AUTHENTICATION_REQUIRED.\n\t\tAuth: map[socks5.METHOD]socks5.Authenticator{\n\t\t\tsocks5.NO_AUTHENTICATION_REQUIRED: \u0026socks5.NoAuth{},\n\t\t\tsocks5.USERNAME_PASSWORD:          \u0026socks5.UserPasswd{Username: \"admin\", Password: \"123456\"},\n\t\t},\n\t}\n\n\t// client send UDP_ASSOCIATE command and get a udp connection.\n\t// Empty local addr string a local address (127.0.0.1:port) is automatically chosen.\n\t// you can specific a address to tell socks server which client address will\n\t// send udp data. Such as clnt.UDPForward(\"127.0.0.1:9999\").\n\tconn, err := clnt.UDPForward(\"\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer conn.Close()\n\n\t// send every datagram should add UDP request header.\n\tsomeData := []byte(\"some data\")\n\t// dest addr where are you send to.\n\tdestAddr, _ := socks5.ParseAddress(\"127.0.0.1:9190\")\n\t// packing socks5 UDP data with dest addr.\n\tpakcedData, err := socks5.PackUDPData(destAddr, someData)\n\t// final send you data\n\tconn.Write(pakcedData)\n\n\t// on the contrary.\n\t// you should unpacked the packet, after received  every packedData.\n\tbuf := make([]byte, 65507)\n\tconn.Read(buf)\n\n\t// unpacking data.\n\tdestAddr, unpackedData, err := socks5.UnpackUDPData(buf)\n\t// operate your udp data. \n\tfmt.Println(unpackedData)\n}\n```\n\n### BIND usage:\n\n```go\npackage main\n\nimport (\n\t\"encoding/binary\"\n\t\"github.com/haochen233/socks5\"\n\t\"log\"\n)\n\nfunc main() {\n\tc := socks5.Client{\n\t\tProxyAddr: \"172.16.1.28:1080\",\n\t\tAuth: map[socks5.METHOD]socks5.Authenticator{\n\t\t\tsocks5.USERNAME_PASSWORD:          \u0026socks5.UserPasswd{\"admin\", \"123456\"},\n\t\t\tsocks5.NO_AUTHENTICATION_REQUIRED: \u0026socks5.NoAuth{},\n\t\t},\n\t}\n\n\t// connect\n\tconn1, err := c.Connect(5, \"127.0.0.1:9000\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tdest := \"127.0.0.1:9001\"\n\t// bind\n\tbindAddr, errors, conn2, err := c.Bind(4, dest)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// An example tell dest about socks server bind address \n\t// via CONNECT proxy connection.\n\tport := make([]byte, 2)\n\tbinary.BigEndian.PutUint16(port, bindAddr.Port)\n\tconn1.Write(append(bindAddr.Addr, port...))\n\n\t// wait the second reply. if nil the dest already\n\t// established with socks server.\n\terr = \u003c-errors\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t\treturn\n\t}\n\n\t// bind success\n\t_, err = conn2.Write([]byte(\"hello\"))\n\tif err != nil {\n\t\treturn\n\t\tlog.Fatal(err)\n\t}\n}\n```\n\n# FAQ:\n- Server default enable socks4. How to disable socks4 support?  \n  when you initialize a socks5 server, you should spefic this flag to disable explicitly.\n   ```go\n   server := \u0026socks5.Server{\n       DisableSocks4: true,\n   }\n   ```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhaochen233%2Fsocks5","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhaochen233%2Fsocks5","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhaochen233%2Fsocks5/lists"}