{"id":13825938,"url":"https://github.com/inconshreveable/go-tunnel","last_synced_at":"2025-04-12T19:31:53.592Z","repository":{"id":13111626,"uuid":"15793299","full_name":"inconshreveable/go-tunnel","owner":"inconshreveable","description":"[DEPRECATED] Tunnel to localhost as a library","archived":false,"fork":false,"pushed_at":"2016-03-14T11:01:31.000Z","size":54,"stargazers_count":159,"open_issues_count":0,"forks_count":28,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-10T12:11:14.658Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/inconshreveable.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":"2014-01-10T09:08:17.000Z","updated_at":"2024-01-29T15:23:13.000Z","dependencies_parsed_at":"2022-07-12T15:08:29.631Z","dependency_job_id":null,"html_url":"https://github.com/inconshreveable/go-tunnel","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inconshreveable%2Fgo-tunnel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inconshreveable%2Fgo-tunnel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inconshreveable%2Fgo-tunnel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inconshreveable%2Fgo-tunnel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/inconshreveable","download_url":"https://codeload.github.com/inconshreveable/go-tunnel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248621150,"owners_count":21134759,"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":[],"created_at":"2024-08-04T09:01:29.513Z","updated_at":"2025-04-12T19:31:53.264Z","avatar_url":"https://github.com/inconshreveable.png","language":"Go","funding_links":[],"categories":["\u003ca id=\"01e6651181d405ecdcd92a452989e7e0\"\u003e\u003c/a\u003e工具"],"sub_categories":["\u003ca id=\"9d6789f22a280f5bb6491d1353b02384\"\u003e\u003c/a\u003e隧道\u0026\u0026穿透"],"readme":"# Project Status\n\nThis library is no longer developed, maintained or supported. Pull requests will be accepted only to ensure\nit continues to compile, all other PRs and issues will be closed.\n\n## go-tunnel\n\nListen for connections made to a remote machine.\n\nYour typical net.Listen() call lets you listen for new connections on a local port. go-tunnel\nprovides an abstraction that lets your application listen for new connections on a port on a remote\nmachine. It also has special support for HTTP listening which allows a client to ask to listen for\nconnections just to a specific HTTP hostname (or on a random one).\n\n## The demo\n\nWe're going to serve a simple HTTP service on a public URL by just adding a single library call.\n\nHere's a simple Go HTTP server that listens on port 9090 of your local machine.\n\n\tpackage main\n\n\timport (\n\t\t\"net/http\"\n\t\t\"io\"\n\t\t\"fmt\"\n\t)\n\n\tfunc main() {\n\t\thttp.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) {\n\t\t\tfmt.Printf(\"Serving hello world page!\\n\")\n\t\t\tio.WriteString(w, \"Hello world!\\n\")\n\t\t})\n\n\t\terr = http.ServeAndListen(\":9090\")\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}\n\nHere's the same simple Go HTTP server that listens on a randomly-assigned public hostname\nassigned by the free service I provide on airlock.io:\n\n\tpackage main\n\n\timport (\n\t\t\"net/http\"\n\t\t\"io\"\n\t\t\"fmt\"\n\t\t\"github.com/inconshreveable/go-tunnel\"\n\t)\n\n\tfunc main() {\n\t\thttp.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) {\n\t\t\tfmt.Printf(\"Serving hello world page!\\n\")\n\t\t\tio.WriteString(w, \"Hello world!\\n\")\n\t\t})\n\n\t\tsess, err := tunnel.Dial(\"example.com:1234\")\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n        tun, err := sess.ListenHTTP()\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tfmt.Printf(\"Serving at: %v\\n\", tun.Addr())\n\n\t\terr = http.Serve(tun, nil)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}\n\nInstead of calling http.ListenAndServe, you call http.Serve and pass in the tunnel listener\nyou create by a call to tunnel.ListenHTTP()\n\n## The Tunnel object\n\nThe coolest part of the library is that calls to Session.Listen(), Session.Listen\\*(), as well as the top\nlevel calls ListenHTTP() and ListenTCP() return a Tunnel object.\n\nTunnel objects *implement net.Listener*. This means you can pass them into http.Serve like in the example above. You\ncan accept new connections from them. If you print the Tunnel.Addr(), you'll get the public address you can\nconnect to the tunnel with.\n\nThe connections returned by the Tunnel.Accept() method even behave like they were accepted on the remote machine.\nIf you print .RemoteAddr() on a connection accepted from a tunnel, you will get back the address of *the remote address of\nthe public connection*.\n\n.Close() a tunnel to return the port or virtual hostname to the server so it may be allocated to another client,\njust like you would with a TCP net.Listener.\n\n## Tunneling over a custom server\n\nIf you want to connect to a custom server instead of using the free public server, you'll need to first\ncreate a session to a different tunneling service.\n\n\tsess, err := tunnel.Dial(\"example.com:1234\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// now bind for remote connections\n\ttun, err := sess.ListenTCP(tcpOptions)\n\n## Customizing listen calls\n\nThe Listen calls on a Session are a little more flexible than the top-level calls. They let you listen with a set of \noptions that can customize the tunnel you bind.\n\n### Binding a specific port\n\nFor TCP tunnels, you may request the server bind a specific port for you instead of a random one:\n\n\timport (\n\t\t\"github.com/inconshreveable/go-tunnel\"\n\t\t\"github.com/inconshreveable/go-tunnel/proto\"\n\t)\n\n\tfunc main() {\n\t\tsess, err := tunnel.Dial(\"example.com:1234\")\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\t// now bind for remote connections\n\t\ttun, err := sess.ListenTCP(\u0026proto.TCPOptions{RemotePort: 12345})\n\t}\n\n### Binding with a specific subdomain and authentication\n\timport (\n\t\t\"github.com/inconshreveable/go-tunnel\"\n\t\t\"github.com/inconshreveable/go-tunnel/proto\"\n\t)\n\n\tfunc main() {\n\t\tsess, err := tunnel.Dial(\"example.com:1234\")\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\t// now bind for remote connections\n\t\ttun, err := sess.ListenHTTP(\u0026proto.HTTPPOptions{Auth: \"user:secret\", Subdomain: \"example\"})\n\t}\n\n## Custom tunnel servers with the server library\n\nThe go-tunnel library also has code that lets you create custom tunneling servers. Typical server\nsetup involves creating a set of binders, and then running the server. This setup would set up a\ntunnel server that listenson port 12345 for new TLS-encrypted tunnel sessions and allows binding\nhttp tunnels only:\n\n\timport (\n\t\t\"github.com/inconshreveable/go-tunnel/server\"\n\t\t\"github.com/inconshreveable/go-tunnel/server/binder\"\n\t)\n\n\tfunc main() {\n\t\tbinders := make(map[string] binders.Binder)\n\n\t\tif binders[\"http\"], err = binder.NewHTTPBinder(\":80\", \"example.com\", 10 * time.Second); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tserver, err := server.ServeTLS(\"tcp\", \":12345\", exampleDotComTlsConfig, binders)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tserver.Run()\n\t}\n\n\nYou can inject custom behavior into the tunneling server by creating a custom set of *server.SessionHooks* and\n*server.TunnelHooks* and setting those properties on your Server object.\n\n## API Documentation\n\nAPI documentation is available on godoc:\n[https://godoc.org/github.com/inconshreveable/go-tunnel](https://godoc.org/github.com/inconshreveable/go-tunnel)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finconshreveable%2Fgo-tunnel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finconshreveable%2Fgo-tunnel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finconshreveable%2Fgo-tunnel/lists"}