{"id":24783384,"url":"https://github.com/chaunsin/goip","last_synced_at":"2025-03-24T06:28:32.876Z","repository":{"id":243215818,"uuid":"811754895","full_name":"chaunsin/goip","owner":"chaunsin","description":"A golang library that get client ip from HTTP request","archived":false,"fork":false,"pushed_at":"2024-06-07T11:47:36.000Z","size":19,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-29T12:16:43.780Z","etag":null,"topics":["clientip","forwarder","golang","http-requests","ip","proxyip","remoteip","x-forwarded-for","x-real-ip"],"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/chaunsin.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-07T08:30:35.000Z","updated_at":"2024-08-20T10:52:21.000Z","dependencies_parsed_at":"2024-06-07T12:11:11.081Z","dependency_job_id":null,"html_url":"https://github.com/chaunsin/goip","commit_stats":null,"previous_names":["chaunsin/goip"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chaunsin%2Fgoip","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chaunsin%2Fgoip/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chaunsin%2Fgoip/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chaunsin%2Fgoip/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chaunsin","download_url":"https://codeload.github.com/chaunsin/goip/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245220847,"owners_count":20579846,"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":["clientip","forwarder","golang","http-requests","ip","proxyip","remoteip","x-forwarded-for","x-real-ip"],"created_at":"2025-01-29T12:16:48.491Z","updated_at":"2025-03-24T06:28:32.858Z","avatar_url":"https://github.com/chaunsin.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# goip\n\n[![GoDoc](https://godoc.org/github.com/chaunsin/goip?status.svg)](https://godoc.org/github.com/chaunsin/goip) [![Go Report Card](https://goreportcard.com/badge/github.com/chaunsin/goip)](https://goreportcard.com/report/github.com/chaunsin/goip)\n\nA golang library that get client ip from HTTP request\n\nThe [gin web framework](https://github.com/gin-gonic/gin) is a very good framework, with its own integration of\nthe [ClientIP()](https://github.com/gin-gonic/gin/blob/64ead9e6bd924d431f4dd612349bc5e13300e6fc/context.go#L824) method,\nbut there will be cases in the actual project without the gin framework, just to use the ClientIP() method code would be\ntoo heavy, so stand on the shoulders of the giants, pull out the relevant code, and do some extensions.\n\n## Features\n\n- X-Real-IP rules are supported\n- Follows the rule of X-Forwarded-For\n- Follows the rule of RFC-7239 standard Forwarded\n- The trusted local or private address is allowed to be configured\n- Allows getting ip from custom headers\n\n## Installation\n\nrequired golang version 1.21+\n\n```shell\ngo get github.com/chaunsin/goip\n```\n\n## Example\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"io\"\n\t\"log\"\n\t\"net/http\"\n\t\"os\"\n\n\t\"github.com/chaunsin/goip\"\n)\n\nfunc Example() {\n\tvar serverAddress = \"127.0.0.1:8080\"\n\n\tmyParse, err := goip.NewParse([]string{\"127.0.0.1\"})\n\tif err != nil {\n\t\tlog.Println(err)\n\t}\n\n\thttp.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) {\n\t\t// Set the global trusted proxy addresses，\n\t\t// If not set, the default priority is used\n\t\tif err := goip.SetTrustedProxies([]string{\"127.0.0.1\"}); err != nil {\n\t\t\tlog.Println(err)\n\t\t}\n\n\t\t// Use the global default configuration\n\t\tip := goip.ClientIP(r)\n\t\t// Use custom configuration\n\t\t_ = myParse.ClientIP(r)\n\n\t\t// Get the ip address from X-Appengine-Remote-Addr first.\n\t\t// If the IP address cannot be obtained, try to get it from another lower priority\n\t\t_ = goip.ClientIP(r, goip.XAppEngineRemoteAddr)\n\t\t_ = myParse.ClientIP(r, goip.XAppEngineRemoteAddr)\n\n\t\t// Use the custom configuration\n\t\t_ = goip.ClientIP(r, goip.XHeader(\"X-My-IP\"))\n\t\t_ = myParse.ClientIP(r)\n\n\t\tfmt.Fprintf(w, \"Your IP address is %s\", ip)\n\t})\n\tgo func() {\n\t\tlog.Println(http.ListenAndServe(serverAddress, nil))\n\t}()\n\n\t// execute http request\n\treq, err := http.NewRequest(\"GET\", \"http://\"+serverAddress, nil)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\t// simulated proxy server added address information\n\treq.Header.Set(\"X-Forwarded-For\", \"123.123.0,1, 123.123.0.2\")\n\tresp, err := http.DefaultClient.Do(req)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tdefer resp.Body.Close()\n\tio.Copy(os.Stdout, resp.Body)\n\n\t// Output:\n\t// Your IP address is 123.123.0.2\n}\n```\n\n## How does it work?\n\nThe basic principle is to look up the specified object value from the [HTTP request header](https://github.com/golang/go/blob/48103d97a84d549b44bc4764df6958f73ba5ee02/src/net/http/request.go#L174).\n\nThe following process is used to search for an ip address:\n\n1. If the user a custom request header(\n   e.g. [CF-Connecting-IP](https://developers.cloudflare.com/fundamentals/reference/http-request-headers/#cf-connecting-ip)\n   、X-Appengine-Remote-Addr...), the fetch is attempted directly from the request header.\n2. Get [RemoteAddr](https://github.com/golang/go/blob/48103d97a84d549b44bc4764df6958f73ba5ee02/src/net/http/request.go#L294) to determine whether the address is in the trusted whitelist, and if so, go to Step 3，Otherwise, go to\n   Step 4 return RemoteAddr\n3. If RemoteAddr is a trusted address, it attempts to get an ip address from X-Real-IP、X-Forward-For、Forwarded, and\n   reverse-searches for the first ip address that is not in the trusted address list\n4. return ip\n\n## Thanks\n\n- https://github.com/gin-gonic/gin\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchaunsin%2Fgoip","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchaunsin%2Fgoip","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchaunsin%2Fgoip/lists"}