{"id":13581124,"url":"https://github.com/pretty66/websocketproxy","last_synced_at":"2025-08-19T09:12:25.872Z","repository":{"id":47733390,"uuid":"396657456","full_name":"pretty66/websocketproxy","owner":"pretty66","description":"🔥🔥🔥Go websocket proxy, a simple websocket reverse proxy implementation, supports ws, wss","archived":false,"fork":false,"pushed_at":"2022-05-07T01:52:22.000Z","size":410,"stargazers_count":78,"open_issues_count":1,"forks_count":22,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T08:52:12.415Z","etag":null,"topics":["go","golang","proxy","websocket","ws","wss"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pretty66.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-08-16T06:41:56.000Z","updated_at":"2025-03-21T00:20:19.000Z","dependencies_parsed_at":"2022-08-27T15:10:36.684Z","dependency_job_id":null,"html_url":"https://github.com/pretty66/websocketproxy","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pretty66/websocketproxy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pretty66%2Fwebsocketproxy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pretty66%2Fwebsocketproxy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pretty66%2Fwebsocketproxy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pretty66%2Fwebsocketproxy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pretty66","download_url":"https://codeload.github.com/pretty66/websocketproxy/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pretty66%2Fwebsocketproxy/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271128831,"owners_count":24703879,"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","status":"online","status_checked_at":"2025-08-19T02:00:09.176Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["go","golang","proxy","websocket","ws","wss"],"created_at":"2024-08-01T15:01:58.346Z","updated_at":"2025-08-19T09:12:25.851Z","avatar_url":"https://github.com/pretty66.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# websocket proxy\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/pretty66/websocketproxy)](https://github.com/pretty66/websocketproxy/blob/master/go.mod)\n\n## [中文](./README_ZH.md)\n\n### Lightweight websocket proxy library\n100 lines of code implements a lightweight websocket proxy library, does not depend on other third-party libraries, and supports ws and wss proxies; if you only need a simple websocket traffic proxy function without any modification to the forwarded content, using this library will be very useful.\n\n## Content\n- [Features](#Features)\n- [Install](#Install)\n- [Use](#Use)\n- [Test](#Test)\n- [Core code](#Core-code)\n- [License](#License)\n\n### Features\n- Extreme performance, almost no performance loss, very low consumption of cpu and memory\n- Support websocket handshake phase for management and control\n- Supports setting header headers (cookie, origin, etc.) in the handshake phase\n- Support ws, wss proxy\n\n### Install\n\u003e go get github.com/pretty66/websocketproxy\n\n### Use\n```go\nimport (\n    \"github.com/pretty66/websocketproxy\"\n    \"net/http\"\n)\n\nwp, err := websocketproxy.NewProxy(\"ws://82.157.123.54:9010/ajaxchattest\", func(r *http.Request) error {\n    // Permission to verify\n    r.Header.Set(\"Cookie\", \"----\")\n    // Source of disguise\n    r.Header.Set(\"Origin\", \"http://82.157.123.54:9010\")\n    return nil\n})\nif err != nil {\n    t.Fatal()\n}\n// proxy path\nhttp.HandleFunc(\"/wsproxy\", wp.Proxy)\nhttp.ListenAndServe(\":9696\", nil)\n```\n\n### Test\nRun the test file and start listening on the `127.0.0.1:9696` port, and use the online testing tool `http://coolaf.com/tool/chattest` to connect to the proxy to test the request response\n\n#### example\n![example](ws_test.png)\n\n\n\n### Core-code\n```go\nfunc (wp *WebsocketProxy) Proxy(writer http.ResponseWriter, request *http.Request) {\n    // Check whether it is a Websocket request\n\tif strings.ToLower(request.Header.Get(\"Connection\")) != \"upgrade\" ||\n\t\tstrings.ToLower(request.Header.Get(\"Upgrade\")) != \"websocket\" {\n\t\t_, _ = writer.Write([]byte(`Must be a websocket request`))\n\t\treturn\n\t}\n    // Hijack connections\n\thijacker, ok := writer.(http.Hijacker)\n\tif !ok {\n\t\treturn\n\t}\n\tconn, _, err := hijacker.Hijack()\n\tif err != nil {\n\t\treturn\n\t}\n\tdefer conn.Close()\n    // Clone request, set destination address path\n\treq := request.Clone(context.TODO())\n\treq.URL.Path, req.URL.RawPath, req.RequestURI = wp.defaultPath, wp.defaultPath, wp.defaultPath\n\treq.Host = wp.remoteAddr\n    // Handshake before callback\n\tif wp.beforeHandshake != nil {\n\t\t// Add headers, permission authentication + masquerade sources\n\t\terr = wp.beforeHandshake(req)\n\t\tif err != nil {\n\t\t\t_, _ = writer.Write([]byte(err.Error()))\n\t\t\treturn\n\t\t}\n\t}\n    // Determine the protocol and select the dialing process\n\tvar remoteConn net.Conn\n\tswitch wp.scheme {\n\tcase WsScheme:\n\t\tremoteConn, err = net.Dial(\"tcp\", wp.remoteAddr)\n\tcase WssScheme:\n\t\tremoteConn, err = tls.Dial(\"tcp\", wp.remoteAddr, wp.tlsc)\n\t}\n\tif err != nil {\n\t\t_, _ = writer.Write([]byte(err.Error()))\n\t\treturn\n\t}\n\tdefer remoteConn.Close()\n\t// Sends a handshake packet to the target WebSocket service\n\terr = req.Write(remoteConn)\n\tif err != nil {\n\t\twp.logger.Println(\"remote write err:\", err)\n\t\treturn\n\t}\n    // Traffic transparent transmission\n\terrChan := make(chan error, 2)\n\tcopyConn := func(a, b net.Conn) {\n\t\t_, err := io.Copy(a, b)\n\t\terrChan \u003c- err\n\t}\n\tgo copyConn(conn, remoteConn) // response\n\tgo copyConn(remoteConn, conn) // request\n\tselect {\n\tcase err = \u003c-errChan:\n\t\tif err != nil {\n\t\t\tlog.Println(err)\n\t\t}\n\t}\n}\n```\n\n### Thanks for free JetBrains Open Source license\n\u003ca href=\"https://www.jetbrains.com/?from=github.com/pretty66/websocketproxy\" target=\"_blank\"\u003e\n\u003cimg src=\"https://user-images.githubusercontent.com/21053373/167233149-c91fb02f-6052-4b65-a0bb-c38df84383cb.png\" height=\"300\"/\u003e\u003c/a\u003e\n\n### License\nwebsocketproxy is under the Apache 2.0 license. See the [LICENSE](./LICENSE) directory for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpretty66%2Fwebsocketproxy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpretty66%2Fwebsocketproxy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpretty66%2Fwebsocketproxy/lists"}