Browse Source

gip: add initial files

Signed-off-by: Nagy Károly Gábriel <k@jpi.io>
master
Nagy Károly Gábriel 6 years ago
commit
d8c63763fd
Signed by: karasz
GPG Key ID: C6BA1070A8CBDA0C
  1. 19
      .editorconfig
  2. 27
      .gitignore
  3. 50
      Gopkg.lock
  4. 34
      Gopkg.toml
  5. 21
      LICENSE
  6. 150
      Makefile
  7. 3
      README.md
  8. 68
      gip.go

19
.editorconfig

@ -0,0 +1,19 @@
# http://editorconfig.org
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_size = 4
indent_style = space
[*.go]
indent_style = tab
indent_size = 4
[Makefile]
indent_style = tab

27
.gitignore vendored

@ -0,0 +1,27 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test
*.prof
/.GOPATH
/bin
/vendor

50
Gopkg.lock generated

@ -0,0 +1,50 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
digest = "1:d64a0aa67f715fd28f5ddfe0d74c06b6d6fe0fed931d77e662f2c4694f6854dc"
name = "github.com/miekg/dns"
packages = ["."]
pruneopts = "UT"
revision = "56be65265e34e731425e0269a301774938827c60"
version = "v1.1.3"
[[projects]]
branch = "master"
digest = "1:d5891c5bca9c62e5d394ca26491d2b710a1dc08cedeb0ca8f9ac4c3305120b02"
name = "golang.org/x/crypto"
packages = [
"ed25519",
"ed25519/internal/edwards25519",
]
pruneopts = "UT"
revision = "ff983b9c42bc9fbf91556e191cc8efb585c16908"
[[projects]]
branch = "master"
digest = "1:19beed19e4246df7aff387a2bcd4519a386e3fe9637690031c4d4b0cf75f7215"
name = "golang.org/x/net"
packages = [
"bpf",
"internal/iana",
"internal/socket",
"ipv4",
"ipv6",
]
pruneopts = "UT"
revision = "915654e7eabcea33ae277abbecf52f0d8b7a9fdc"
[[projects]]
branch = "master"
digest = "1:f6f6d9d6f80c9e32acc0f4f3b904e0b9f5b18a7757c2ef941121bd79a10e7bdf"
name = "golang.org/x/sys"
packages = ["unix"]
pruneopts = "UT"
revision = "11f53e03133963fb11ae0588e08b5e0b85be8be5"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
input-imports = ["github.com/miekg/dns"]
solver-name = "gps-cdcl"
solver-version = 1

34
Gopkg.toml

@ -0,0 +1,34 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true
[[constraint]]
name = "github.com/miekg/dns"
version = "1.1.3"
[prune]
go-tests = true
unused-packages = true

21
LICENSE

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019 Nagy Károly Gábriel
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

150
Makefile

@ -0,0 +1,150 @@
# The import path is where your repository can be found.
# To import subpackages, always prepend the full import path.
# If you change this, run `make clean`. Read more: https://git.io/vM7zV
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
current_dir := $(notdir $(patsubst %/,%,$(dir $(mkfile_path))))
IMPORT_PATH := git.jpi.io/$(USER)/$(current_dir)
# V := 1 # When V is set, print commands and build progress.
# Space separated patterns of packages to skip in list, test, format.
IGNORED_PACKAGES := /vendor/
.PHONY: all
all: build
.PHONY: build
build: .GOPATH/.ok
$Q go install $(if $V,-v) $(VERSION_FLAGS) $(IMPORT_PATH)
### Code not in the repository root? Another binary? Add to the path like this.
# .PHONY: otherbin
# otherbin: .GOPATH/.ok
# $Q go install $(if $V,-v) $(VERSION_FLAGS) $(IMPORT_PATH)/cmd/otherbin
##### ^^^^^^ EDIT ABOVE ^^^^^^ #####
##### =====> Utility targets <===== #####
.PHONY: clean test list cover format gen
clean:
$Q rm -rf bin .GOPATH
test: .GOPATH/.ok
$Q go test $(if $V,-v) -i -race $(allpackages) # install -race libs to speed up next run
ifndef CI
$Q go vet $(allpackages)
$Q GODEBUG=cgocheck=2 go test -race $(allpackages)
else
$Q ( go vet $(allpackages); echo $$? ) | \
tee .GOPATH/test/vet.txt | sed '$$ d'; exit $$(tail -1 .GOPATH/test/vet.txt)
$Q ( GODEBUG=cgocheck=2 go test -v -race $(allpackages); echo $$? ) | \
tee .GOPATH/test/output.txt | sed '$$ d'; exit $$(tail -1 .GOPATH/test/output.txt)
endif
list: .GOPATH/.ok
@echo $(allpackages)
cover: bin/gocovmerge .GOPATH/.ok
@echo "NOTE: make cover does not exit 1 on failure, don't use it to check for tests success!"
$Q rm -f .GOPATH/cover/*.out .GOPATH/cover/all.merged
$(if $V,@echo "-- go test -coverpkg=./... -coverprofile=.GOPATH/cover/... ./...")
@for MOD in $(allpackages); do \
go test -coverpkg=`echo $(allpackages)|tr " " ","` \
-coverprofile=.GOPATH/cover/unit-`echo $$MOD|tr "/" "_"`.out \
$$MOD 2>&1 | grep -v "no packages being tested depend on"; \
done
$Q ./bin/gocovmerge .GOPATH/cover/*.out > .GOPATH/cover/all.merged
ifndef CI
$Q go tool cover -html .GOPATH/cover/all.merged
else
$Q go tool cover -html .GOPATH/cover/all.merged -o .GOPATH/cover/all.html
endif
@echo ""
@echo "=====> Total test coverage: <====="
@echo ""
$Q go tool cover -func .GOPATH/cover/all.merged
format: bin/goimports .GOPATH/.ok
$Q find .GOPATH/src/$(IMPORT_PATH)/ -iname \*.go | grep -v \
-e "^$$" $(addprefix -e ,$(IGNORED_PACKAGES)) | xargs ./bin/goimports -w
gen: .GOPATH/.ok
@echo "Running go generate"
$Q cd $(CURDIR)/.GOPATH/src/$(IMPORT_PATH) && go generate
@echo "Done!"
##### =====> Internals <===== #####
.PHONY: setup
setup: clean .GOPATH/.ok
@if ! grep "/.GOPATH" .gitignore > /dev/null 2>&1; then \
echo "/.GOPATH" >> .gitignore; \
echo "/bin" >> .gitignore; \
fi
go get -u github.com/golang/dep/cmd/dep
- go get -u golang.org/x/tools/cmd/goimports
- go get -u github.com/wadey/gocovmerge
@test -f Gopkg.toml || \
(cd $(CURDIR)/.GOPATH/src/$(IMPORT_PATH) && ./bin/dep init)
(cd $(CURDIR)/.GOPATH/src/$(IMPORT_PATH) && ./bin/dep ensure)
VERSION := $(shell git describe --tags --always --dirty="-dev")
DATE := $(shell date -u '+%Y-%m-%d-%H%M UTC')
VERSION_FLAGS := -ldflags='-X "main.Version=$(VERSION)" -X "main.BuildTime=$(DATE)"'
# cd into the GOPATH to workaround ./... not following symlinks
_allpackages = $(shell ( cd $(CURDIR)/.GOPATH/src/$(IMPORT_PATH) && \
GOPATH=$(CURDIR)/.GOPATH go list ./... 2>&1 1>&3 | \
grep -v -e "^$$" $(addprefix -e ,$(IGNORED_PACKAGES)) 1>&2 ) 3>&1 | \
grep -v -e "^$$" $(addprefix -e ,$(IGNORED_PACKAGES)))
# memoize allpackages, so that it's executed only once and only if used
allpackages = $(if $(__allpackages),,$(eval __allpackages := $$(_allpackages)))$(__allpackages)
export GOPATH := $(CURDIR)/.GOPATH
unexport GOBIN
Q := $(if $V,,@)
.GOPATH/.ok:
$Q mkdir -p "$(dir .GOPATH/src/$(IMPORT_PATH))"
$Q ln -s ../../../.. ".GOPATH/src/$(IMPORT_PATH)"
$Q mkdir -p .GOPATH/test .GOPATH/cover
$Q mkdir -p bin
$Q ln -s ../bin .GOPATH/bin
$Q touch $@
.PHONY: bin/gocovmerge bin/goimports
bin/gocovmerge: .GOPATH/.ok
@test -d ./vendor/github.com/wadey/gocovmerge || \
{ echo "Vendored gocovmerge not found, try running 'make setup'..."; exit 1; }
$Q go install $(IMPORT_PATH)/vendor/github.com/wadey/gocovmerge
bin/goimports: .GOPATH/.ok
@test -d ./vendor/golang.org/x/tools/cmd/goimports || \
{ echo "Vendored goimports not found, try running 'make setup'..."; exit 1; }
$Q go install $(IMPORT_PATH)/vendor/golang.org/x/tools/cmd/goimports
# Based on https://github.com/cloudflare/hellogopher - v1.1 - MIT License
#
# Copyright (c) 2017 Cloudflare
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

3
README.md

@ -0,0 +1,3 @@
# gip
Find your public IP

68
gip.go

@ -0,0 +1,68 @@
package main
import (
"fmt"
"github.com/miekg/dns"
)
/*
dig {-4,-6} +short myip.opendns.com @resolver1.opendns.com
dig {-4,-6} TXT +short o-o.myaddr.l.google.com @ns1.google.com
*/
func get4(c *dns.Client, m *dns.Msg, nameserver string) (*dns.Msg, error) {
c.Net = "udp4"
m.SetQuestion("myip.opendns.com.", dns.TypeA)
r, _, err := c.Exchange(m, nameserver)
return r, err
}
func get6(c *dns.Client, m *dns.Msg, nameserver string) (*dns.Msg, error) {
c.Net = "udp6"
m.SetQuestion("myip.opendns.com.", dns.TypeAAAA)
r, _, err := c.Exchange(m, nameserver)
return r, err
}
func printIP(ra []dns.RR) []string {
var ips []string
for _, ansa := range ra {
switch ansb := ansa.(type) {
case *dns.A:
ips = append(ips, ansb.A.String())
case *dns.AAAA:
ips = append(ips, ansb.AAAA.String())
}
}
return ips
}
func main() {
nameserver := dns.Fqdn("resolver1.opendns.com") + ":53"
c := new(dns.Client)
c.Net = "udp4"
m := &dns.Msg{
MsgHdr: dns.MsgHdr{
RecursionDesired: true,
},
Question: make([]dns.Question, 1),
}
r4, err := get4(c, m, nameserver)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(printIP(r4.Answer))
}
r6, e6 := get6(c, m, nameserver)
if err != nil {
fmt.Println(e6)
} else {
fmt.Println(printIP(r6.Answer))
}
}
Loading…
Cancel
Save