-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add dockerfile & support myip command (#13)
- Loading branch information
Showing
15 changed files
with
755 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
FROM golang:1.21 as builder | ||
|
||
COPY . /building | ||
WORKDIR /building | ||
RUN make build | ||
|
||
FROM alpine:3 as alpine | ||
|
||
WORKDIR / | ||
COPY --from=builder /building/bin/ips /bin/ips | ||
EXPOSE 6860 | ||
ENTRYPOINT ["/bin/ips", "server"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) 2023 [email protected] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package ips | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(myipCmd) | ||
|
||
// myip | ||
myipCmd.Flags().StringVarP(&localAddr, "local-addr", "", "", "Specify local address for outbound connections.") | ||
myipCmd.Flags().IntVarP(&myIPCount, "count", "", 0, "Number of detectors to confirm the IP.") | ||
myipCmd.Flags().IntVarP(&myIPTimeoutS, "timeout", "", 0, "Set the maximum wait time for detectors.") | ||
|
||
// operate | ||
myipCmd.Flags().StringVarP(&fields, "fields", "f", "", "Specify the fields of interest for the IP data. Separate multiple fields with commas.") | ||
myipCmd.Flags().BoolVarP(&useDBFields, "use-db-fields", "", false, "Use field names as they appear in the database. Default is common field names.") | ||
myipCmd.Flags().StringVarP(&rewriteFiles, "rewrite-files", "r", "", "List of files that need to be rewritten based on the given configurations.") | ||
myipCmd.Flags().StringVarP(&lang, "lang", "", "", "Set the language for the output. Example values: en, zh-CN, etc.") | ||
|
||
// database | ||
myipCmd.Flags().StringVarP(&rootFile, "file", "i", "", "Path to the IPv4 and IPv6 database file.") | ||
myipCmd.Flags().StringVarP(&rootFormat, "format", "", "", "Specify the format of the database. Examples: ipdb, mmdb, etc.") | ||
myipCmd.Flags().StringVarP(&rootIPv4File, "ipv4-file", "", "", "Path to the IPv4 database file.") | ||
myipCmd.Flags().StringVarP(&rootIPv4Format, "ipv4-format", "", "", "Specify the format for IPv4 data. Examples: ipdb, mmdb, etc.") | ||
myipCmd.Flags().StringVarP(&rootIPv6File, "ipv6-file", "", "", "Path to the IPv6 database file.") | ||
myipCmd.Flags().StringVarP(&rootIPv6Format, "ipv6-format", "", "", "Specify the format for IPv6 data. Examples: ipdb, mmdb, etc.") | ||
myipCmd.Flags().StringVarP(&readerOption, "database-option", "", "", "Specify the option for database reader.") | ||
|
||
// output | ||
myipCmd.Flags().StringVarP(&rootTextFormat, "text-format", "", "", "Specify the desired format for text output. It supports %origin and %values parameters.") | ||
myipCmd.Flags().StringVarP(&rootTextValuesSep, "text-values-sep", "", "", "Specify the separator for values in text output. (default is space)") | ||
myipCmd.Flags().BoolVarP(&rootJson, "json", "j", false, "Output the results in JSON format.") | ||
myipCmd.Flags().BoolVarP(&rootJsonIndent, "json-indent", "", false, "Output the results in indent JSON format.") | ||
} | ||
|
||
var myipCmd = &cobra.Command{ | ||
Use: "myip", | ||
Short: "Retrieve your public IP address.", | ||
Long: `The 'myip' command uses multiple detectors to discover and return your public IP address. | ||
It is designed to work in scenarios with multiple network interfaces and allows you to specify | ||
the local address for outbound connections, the number of detectors to confirm the IP, and the timeout | ||
for the detection process.`, | ||
PreRun: PreRunInit, | ||
Run: MyIP, | ||
} | ||
|
||
func MyIP(cmd *cobra.Command, args []string) { | ||
ip, err := manager.MyIP() | ||
if err != nil { | ||
return | ||
} | ||
cmd.Println(ip) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright (c) 2023 [email protected] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package ips | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/sjzar/ips/internal/myip" | ||
) | ||
|
||
func (m *Manager) MyIP() (string, error) { | ||
ip, err := myip.GetPublicIP(m.Conf.LocalAddr, m.Conf.MyIPCount, m.Conf.MyIPTimeoutS) | ||
if err != nil { | ||
log.Debugf("myip.GetPublicIP error: %v", err) | ||
return "", err | ||
} | ||
|
||
return m.ParseText(ip.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright (c) 2023 [email protected] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package myip | ||
|
||
import ( | ||
"context" | ||
"net" | ||
) | ||
|
||
type Detector interface { | ||
Discover(ctx context.Context, localAddr string) (ip net.IP, err error) | ||
} | ||
|
||
var Detectors = []Detector{ | ||
// STUN servers | ||
&STUNDetector{Host: "stun:stun.l.google.com:19302"}, | ||
&STUNDetector{Host: "stun:stun1.l.google.com:19302"}, | ||
&STUNDetector{Host: "stun:stun2.l.google.com:19302"}, | ||
&STUNDetector{Host: "stun:stun3.l.google.com:19302"}, | ||
&STUNDetector{Host: "stun:stun4.l.google.com:19302"}, | ||
&STUNDetector{Host: "stun:stun.aa.net.uk:3478"}, | ||
&STUNDetector{Host: "stun:stun.hoiio.com:3478"}, | ||
&STUNDetector{Host: "stun:stun.acrobits.cz:3478"}, | ||
&STUNDetector{Host: "stun:stun.voip.blackberry.com:3478"}, | ||
&STUNDetector{Host: "stun:stun.sip.us:3478"}, | ||
&STUNDetector{Host: "stun:stun.antisip.com:3478"}, | ||
&STUNDetector{Host: "stun:stun.avigora.fr:3478"}, | ||
&STUNDetector{Host: "stun:stun.linphone.org:3478"}, | ||
&STUNDetector{Host: "stun:stun.voipgate.com:3478"}, | ||
&STUNDetector{Host: "stun:stun.cope.es:3478"}, | ||
&STUNDetector{Host: "stun:stun.bluesip.net:3478"}, | ||
&STUNDetector{Host: "stun:stun.solcon.nl:3478"}, | ||
&STUNDetector{Host: "stun:stun.uls.co.za:3478"}, | ||
|
||
// HTTP servers | ||
&HTTPDetector{Host: "http://inet-ip.info/ip"}, | ||
&HTTPDetector{Host: "http://whatismyip.akamai.com/"}, | ||
&HTTPDetector{Host: "https://ipecho.net/plain"}, | ||
&HTTPDetector{Host: "https://eth0.me/"}, | ||
&HTTPDetector{Host: "https://ifconfig.me/ip"}, | ||
&HTTPDetector{Host: "https://checkip.amazonaws.com/"}, | ||
&HTTPDetector{Host: "https://wgetip.com/"}, | ||
&HTTPDetector{Host: "https://ip.tyk.nu/"}, | ||
&HTTPDetector{Host: "https://l2.io/ip"}, | ||
&HTTPDetector{Host: "https://api.ipify.org/"}, | ||
&HTTPDetector{Host: "https://myexternalip.com/raw"}, | ||
&HTTPDetector{Host: "https://icanhazip.com"}, | ||
&HTTPDetector{Host: "https://ifconfig.io/ip"}, | ||
&HTTPDetector{Host: "https://ifconfig.co/ip"}, | ||
&HTTPDetector{Host: "https://ipinfo.io/ip"}, | ||
&HTTPDetector{Host: "https://wtfismyip.com/text"}, | ||
|
||
// DNS servers | ||
&DNSDetector{Domain: "myip.opendns.com.", Server: "resolver1.opendns.com:53", QueryType: "A"}, | ||
&DNSDetector{Domain: "myip.opendns.com.", Server: "resolver2.opendns.com:53", QueryType: "A"}, | ||
&DNSDetector{Domain: "myip.opendns.com.", Server: "resolver3.opendns.com:53", QueryType: "A"}, | ||
&DNSDetector{Domain: "myip.opendns.com.", Server: "resolver4.opendns.com:53", QueryType: "A"}, | ||
&DNSDetector{Domain: "whoami.akamai.net.", Server: "ns1-1.akamaitech.net:53", QueryType: "A"}, | ||
&DNSDetector{Domain: "whoami.ultradns.net.", Server: "pdns1.ultradns.net:53", QueryType: "A"}, | ||
&DNSDetector{Domain: "o-o.myaddr.l.google.com.", Server: "ns1.google.com:53", QueryType: "TXT"}, | ||
} |
Oops, something went wrong.