Skip to content

Commit 659169c

Browse files
authored
add dockerfile & support myip command (#13)
1 parent 7ea462f commit 659169c

File tree

15 files changed

+755
-7
lines changed

15 files changed

+755
-7
lines changed

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM golang:1.21 as builder
2+
3+
COPY . /building
4+
WORKDIR /building
5+
RUN make build
6+
7+
FROM alpine:3 as alpine
8+
9+
WORKDIR /
10+
COPY --from=builder /building/bin/ips /bin/ips
11+
EXPOSE 6860
12+
ENTRYPOINT ["/bin/ips", "server"]

cmd/ips/cmd_myip.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2023 [email protected]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package ips
18+
19+
import (
20+
"github.com/spf13/cobra"
21+
)
22+
23+
func init() {
24+
rootCmd.AddCommand(myipCmd)
25+
26+
// myip
27+
myipCmd.Flags().StringVarP(&localAddr, "local-addr", "", "", "Specify local address for outbound connections.")
28+
myipCmd.Flags().IntVarP(&myIPCount, "count", "", 0, "Number of detectors to confirm the IP.")
29+
myipCmd.Flags().IntVarP(&myIPTimeoutS, "timeout", "", 0, "Set the maximum wait time for detectors.")
30+
31+
// operate
32+
myipCmd.Flags().StringVarP(&fields, "fields", "f", "", "Specify the fields of interest for the IP data. Separate multiple fields with commas.")
33+
myipCmd.Flags().BoolVarP(&useDBFields, "use-db-fields", "", false, "Use field names as they appear in the database. Default is common field names.")
34+
myipCmd.Flags().StringVarP(&rewriteFiles, "rewrite-files", "r", "", "List of files that need to be rewritten based on the given configurations.")
35+
myipCmd.Flags().StringVarP(&lang, "lang", "", "", "Set the language for the output. Example values: en, zh-CN, etc.")
36+
37+
// database
38+
myipCmd.Flags().StringVarP(&rootFile, "file", "i", "", "Path to the IPv4 and IPv6 database file.")
39+
myipCmd.Flags().StringVarP(&rootFormat, "format", "", "", "Specify the format of the database. Examples: ipdb, mmdb, etc.")
40+
myipCmd.Flags().StringVarP(&rootIPv4File, "ipv4-file", "", "", "Path to the IPv4 database file.")
41+
myipCmd.Flags().StringVarP(&rootIPv4Format, "ipv4-format", "", "", "Specify the format for IPv4 data. Examples: ipdb, mmdb, etc.")
42+
myipCmd.Flags().StringVarP(&rootIPv6File, "ipv6-file", "", "", "Path to the IPv6 database file.")
43+
myipCmd.Flags().StringVarP(&rootIPv6Format, "ipv6-format", "", "", "Specify the format for IPv6 data. Examples: ipdb, mmdb, etc.")
44+
myipCmd.Flags().StringVarP(&readerOption, "database-option", "", "", "Specify the option for database reader.")
45+
46+
// output
47+
myipCmd.Flags().StringVarP(&rootTextFormat, "text-format", "", "", "Specify the desired format for text output. It supports %origin and %values parameters.")
48+
myipCmd.Flags().StringVarP(&rootTextValuesSep, "text-values-sep", "", "", "Specify the separator for values in text output. (default is space)")
49+
myipCmd.Flags().BoolVarP(&rootJson, "json", "j", false, "Output the results in JSON format.")
50+
myipCmd.Flags().BoolVarP(&rootJsonIndent, "json-indent", "", false, "Output the results in indent JSON format.")
51+
}
52+
53+
var myipCmd = &cobra.Command{
54+
Use: "myip",
55+
Short: "Retrieve your public IP address.",
56+
Long: `The 'myip' command uses multiple detectors to discover and return your public IP address.
57+
It is designed to work in scenarios with multiple network interfaces and allows you to specify
58+
the local address for outbound connections, the number of detectors to confirm the IP, and the timeout
59+
for the detection process.`,
60+
PreRun: PreRunInit,
61+
Run: MyIP,
62+
}
63+
64+
func MyIP(cmd *cobra.Command, args []string) {
65+
ip, err := manager.MyIP()
66+
if err != nil {
67+
return
68+
}
69+
cmd.Println(ip)
70+
}

cmd/ips/config.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ var (
112112
// writerOption specifies the options for the writer.
113113
writerOption string
114114

115+
// myip
116+
// localAddr specifies the local address (in IP format) that should be used for outbound connections.
117+
// Useful in systems with multiple network interfaces.
118+
localAddr string
119+
120+
// myIPCount defines the minimum number of detectors that should return the same IP
121+
// for the IP to be considered as the system's public IP.
122+
myIPCount int
123+
124+
// myIPTimeoutS specifies the maximum duration (in seconds) to wait for the detectors to return an IP.
125+
myIPTimeoutS int
126+
115127
// server
116128

117129
// addr specifies the server address.
@@ -203,6 +215,18 @@ func GetFlagConfig() *ips.Config {
203215
conf.Addr = addr
204216
}
205217

218+
if len(localAddr) != 0 {
219+
conf.LocalAddr = localAddr
220+
}
221+
222+
if myIPCount != 0 {
223+
conf.MyIPCount = myIPCount
224+
}
225+
226+
if myIPTimeoutS != 0 {
227+
conf.MyIPTimeoutS = myIPTimeoutS
228+
}
229+
206230
return conf
207231
}
208232

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ require (
66
github.com/dilfish/awdb-golang/awdb-golang v1.0.20210701
77
github.com/gin-gonic/gin v1.9.1
88
github.com/maxmind/mmdbwriter v1.0.0
9+
github.com/miekg/dns v1.1.41
910
github.com/oschwald/maxminddb-golang v1.12.0
11+
github.com/pion/stun/v2 v2.0.0
1012
github.com/schollz/progressbar/v3 v3.13.1
1113
github.com/sirupsen/logrus v1.9.3
1214
github.com/spf13/cobra v1.6.1
1315
github.com/spf13/viper v1.14.0
1416
github.com/stretchr/testify v1.8.4
15-
golang.org/x/text v0.9.0
17+
golang.org/x/text v0.12.0
1618
)

go.sum

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5
468468
github.com/maxmind/mmdbwriter v1.0.0 h1:bieL4P6yaYaHvbtLSwnKtEvScUKKD6jcKaLiTM3WSMw=
469469
github.com/maxmind/mmdbwriter v1.0.0/go.mod h1:noBMCUtyN5PUQ4H8ikkOvGSHhzhLok51fON2hcrpKj8=
470470
github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
471+
github.com/miekg/dns v1.1.41 h1:WMszZWJG0XmzbK9FEmzH2TVcqYzFesusSIB41b8KHxY=
471472
github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI=
472473
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
473474
github.com/mitchellh/cli v1.1.0/go.mod h1:xcISNoH86gajksDmfB23e/pu+B+GeFRMYmoHXxx3xhI=
@@ -497,6 +498,16 @@ github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCko
497498
github.com/pelletier/go-toml/v2 v2.0.5/go.mod h1:OMHamSCAODeSsVrwwvcJOaoN0LIUIaFVNZzmWyNfXas=
498499
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
499500
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
501+
github.com/pion/dtls/v2 v2.2.7 h1:cSUBsETxepsCSFSxC3mc/aDo14qQLMSL+O6IjG28yV8=
502+
github.com/pion/dtls/v2 v2.2.7/go.mod h1:8WiMkebSHFD0T+dIU+UeBaoV7kDhOW5oDCzZ7WZ/F9s=
503+
github.com/pion/logging v0.2.2 h1:M9+AIj/+pxNsDfAT64+MAVgJO0rsyLnoJKCqf//DoeY=
504+
github.com/pion/logging v0.2.2/go.mod h1:k0/tDVsRCX2Mb2ZEmTqNa7CWsQPc+YYCB7Q+5pahoms=
505+
github.com/pion/stun/v2 v2.0.0 h1:A5+wXKLAypxQri59+tmQKVs7+l6mMM+3d+eER9ifRU0=
506+
github.com/pion/stun/v2 v2.0.0/go.mod h1:22qRSh08fSEttYUmJZGlriq9+03jtVmXNODgLccj8GQ=
507+
github.com/pion/transport/v2 v2.2.1 h1:7qYnCBlpgSJNYMbLCKuSY9KbQdBFoETvPNETv0y4N7c=
508+
github.com/pion/transport/v2 v2.2.1/go.mod h1:cXXWavvCnFF6McHTft3DWS9iic2Mftcz1Aq29pGcU5g=
509+
github.com/pion/transport/v3 v3.0.1 h1:gDTlPJwROfSfz6QfSi0ZmeCSkFcnWWiiR9ES0ouANiM=
510+
github.com/pion/transport/v3 v3.0.1/go.mod h1:UY7kiITrlMv7/IKgd5eTUcaahZx5oUN3l9SzK5f5xE0=
500511
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
501512
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
502513
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -622,8 +633,10 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
622633
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
623634
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
624635
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
625-
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
636+
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
626637
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
638+
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
639+
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
627640
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
628641
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
629642
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -718,8 +731,10 @@ golang.org/x/net v0.0.0-20220909164309-bea034e7d591/go.mod h1:YDH+HFinaLZZlnHAfS
718731
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
719732
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
720733
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
721-
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
734+
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
722735
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
736+
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
737+
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
723738
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
724739
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
725740
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -758,6 +773,7 @@ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJ
758773
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
759774
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
760775
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
776+
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
761777
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
762778
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
763779
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@@ -848,15 +864,19 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
848864
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
849865
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
850866
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
867+
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
851868
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
852-
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
853869
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
870+
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
871+
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
854872
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
855873
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
856874
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
857875
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
858-
golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=
876+
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
859877
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
878+
golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0=
879+
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
860880
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
861881
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
862882
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -870,8 +890,9 @@ golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
870890
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
871891
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
872892
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
873-
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
874893
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
894+
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
895+
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
875896
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
876897
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
877898
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=

internal/ips/config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ type Config struct {
100100
// WriterOption specifies the options for the writer.
101101
WriterOption string `mapstructure:"writer_option"`
102102

103+
// MyIP
104+
// LocalAddr specifies the local address (in IP format) that should be used for outbound connections.
105+
// Useful in systems with multiple network interfaces.
106+
LocalAddr string `mapstructure:"local_addr"`
107+
108+
// MyIPCount defines the minimum number of detectors that should return the same IP
109+
// for the IP to be considered as the system's public IP.
110+
MyIPCount int `mapstructure:"my_ip_count" default:"3"`
111+
112+
// MyIPTimeoutS specifies the maximum duration (in seconds) to wait for the detectors to return an IP.
113+
MyIPTimeoutS int `mapstructure:"myip_timeout_s"`
114+
103115
// Service
104116
// Addr specifies the address for the service.
105117
Addr string `mapstructure:"addr" default:":6860"`

internal/ips/myip.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2023 [email protected]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package ips
18+
19+
import (
20+
log "github.com/sirupsen/logrus"
21+
22+
"github.com/sjzar/ips/internal/myip"
23+
)
24+
25+
func (m *Manager) MyIP() (string, error) {
26+
ip, err := myip.GetPublicIP(m.Conf.LocalAddr, m.Conf.MyIPCount, m.Conf.MyIPTimeoutS)
27+
if err != nil {
28+
log.Debugf("myip.GetPublicIP error: %v", err)
29+
return "", err
30+
}
31+
32+
return m.ParseText(ip.String())
33+
}

internal/myip/detector.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2023 [email protected]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package myip
18+
19+
import (
20+
"context"
21+
"net"
22+
)
23+
24+
type Detector interface {
25+
Discover(ctx context.Context, localAddr string) (ip net.IP, err error)
26+
}
27+
28+
var Detectors = []Detector{
29+
// STUN servers
30+
&STUNDetector{Host: "stun:stun.l.google.com:19302"},
31+
&STUNDetector{Host: "stun:stun1.l.google.com:19302"},
32+
&STUNDetector{Host: "stun:stun2.l.google.com:19302"},
33+
&STUNDetector{Host: "stun:stun3.l.google.com:19302"},
34+
&STUNDetector{Host: "stun:stun4.l.google.com:19302"},
35+
&STUNDetector{Host: "stun:stun.aa.net.uk:3478"},
36+
&STUNDetector{Host: "stun:stun.hoiio.com:3478"},
37+
&STUNDetector{Host: "stun:stun.acrobits.cz:3478"},
38+
&STUNDetector{Host: "stun:stun.voip.blackberry.com:3478"},
39+
&STUNDetector{Host: "stun:stun.sip.us:3478"},
40+
&STUNDetector{Host: "stun:stun.antisip.com:3478"},
41+
&STUNDetector{Host: "stun:stun.avigora.fr:3478"},
42+
&STUNDetector{Host: "stun:stun.linphone.org:3478"},
43+
&STUNDetector{Host: "stun:stun.voipgate.com:3478"},
44+
&STUNDetector{Host: "stun:stun.cope.es:3478"},
45+
&STUNDetector{Host: "stun:stun.bluesip.net:3478"},
46+
&STUNDetector{Host: "stun:stun.solcon.nl:3478"},
47+
&STUNDetector{Host: "stun:stun.uls.co.za:3478"},
48+
49+
// HTTP servers
50+
&HTTPDetector{Host: "http://inet-ip.info/ip"},
51+
&HTTPDetector{Host: "http://whatismyip.akamai.com/"},
52+
&HTTPDetector{Host: "https://ipecho.net/plain"},
53+
&HTTPDetector{Host: "https://eth0.me/"},
54+
&HTTPDetector{Host: "https://ifconfig.me/ip"},
55+
&HTTPDetector{Host: "https://checkip.amazonaws.com/"},
56+
&HTTPDetector{Host: "https://wgetip.com/"},
57+
&HTTPDetector{Host: "https://ip.tyk.nu/"},
58+
&HTTPDetector{Host: "https://l2.io/ip"},
59+
&HTTPDetector{Host: "https://api.ipify.org/"},
60+
&HTTPDetector{Host: "https://myexternalip.com/raw"},
61+
&HTTPDetector{Host: "https://icanhazip.com"},
62+
&HTTPDetector{Host: "https://ifconfig.io/ip"},
63+
&HTTPDetector{Host: "https://ifconfig.co/ip"},
64+
&HTTPDetector{Host: "https://ipinfo.io/ip"},
65+
&HTTPDetector{Host: "https://wtfismyip.com/text"},
66+
67+
// DNS servers
68+
&DNSDetector{Domain: "myip.opendns.com.", Server: "resolver1.opendns.com:53", QueryType: "A"},
69+
&DNSDetector{Domain: "myip.opendns.com.", Server: "resolver2.opendns.com:53", QueryType: "A"},
70+
&DNSDetector{Domain: "myip.opendns.com.", Server: "resolver3.opendns.com:53", QueryType: "A"},
71+
&DNSDetector{Domain: "myip.opendns.com.", Server: "resolver4.opendns.com:53", QueryType: "A"},
72+
&DNSDetector{Domain: "whoami.akamai.net.", Server: "ns1-1.akamaitech.net:53", QueryType: "A"},
73+
&DNSDetector{Domain: "whoami.ultradns.net.", Server: "pdns1.ultradns.net:53", QueryType: "A"},
74+
&DNSDetector{Domain: "o-o.myaddr.l.google.com.", Server: "ns1.google.com:53", QueryType: "TXT"},
75+
}

0 commit comments

Comments
 (0)