站点更新:2017-12-22 16:01:46
This commit is contained in:
parent
afb873ed2b
commit
3becc3a08d
52
common/ip.go
52
common/ip.go
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -29,56 +29,6 @@ type IP struct {
|
||||
Isp string `json:"isp"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
external_ip := get_external()
|
||||
|
||||
external_ip = strings.Replace(external_ip, "\n", "", -1)
|
||||
fmt.Println("公网ip是: ", external_ip)
|
||||
|
||||
fmt.Println("------Dividing Line------")
|
||||
|
||||
ip := net.ParseIP(external_ip)
|
||||
if ip == nil {
|
||||
fmt.Println("您输入的不是有效的IP地址,请重新输入!")
|
||||
} else {
|
||||
result := TabaoAPI(string(external_ip))
|
||||
if result != nil {
|
||||
fmt.Println("国家:", result.Data.Country)
|
||||
fmt.Println("地区:", result.Data.Area)
|
||||
fmt.Println("城市:", result.Data.City)
|
||||
fmt.Println("运营商:", result.Data.Isp)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("------Dividing Line------GetIntranetIp")
|
||||
|
||||
GetIntranetIp()
|
||||
|
||||
fmt.Println("------Dividing Line------")
|
||||
|
||||
ip_int := inet_aton(net.ParseIP(external_ip))
|
||||
fmt.Println("Convert IPv4 address to decimal number(base 10) :", ip_int)
|
||||
|
||||
ip_result := inet_ntoa(ip_int)
|
||||
fmt.Println("Convert decimal number(base 10) to IPv4 address:", ip_result)
|
||||
|
||||
fmt.Println("------Dividing Line------")
|
||||
|
||||
is_between := IpBetween(net.ParseIP("0.0.0.0"), net.ParseIP("255.255.255.255"), net.ParseIP(external_ip))
|
||||
fmt.Println("check result: ", is_between)
|
||||
|
||||
fmt.Println("------Dividing Line------external_ip")
|
||||
is_public_ip := IsPublicIP(net.ParseIP(external_ip))
|
||||
fmt.Println("It is public ip: ", is_public_ip)
|
||||
|
||||
is_public_ip = IsPublicIP(net.ParseIP("169.254.85.131"))
|
||||
fmt.Println("It is public ip: ", is_public_ip)
|
||||
|
||||
fmt.Println("------Dividing Line------GetPulicIP")
|
||||
fmt.Println(GetPulicIP())
|
||||
}
|
||||
|
||||
func get_external() string {
|
||||
resp, err := http.Get("http://myexternalip.com/raw")
|
||||
if err != nil {
|
||||
|
58
common/ip_test.go
Normal file
58
common/ip_test.go
Normal file
@ -0,0 +1,58 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIp(t *testing.T) {
|
||||
|
||||
external_ip := get_external()
|
||||
|
||||
external_ip = strings.Replace(external_ip, "\n", "", -1)
|
||||
fmt.Println("公网ip是: ", external_ip)
|
||||
|
||||
fmt.Println("------Dividing Line------")
|
||||
|
||||
ip := net.ParseIP(external_ip)
|
||||
if ip == nil {
|
||||
fmt.Println("您输入的不是有效的IP地址,请重新输入!")
|
||||
} else {
|
||||
result := TabaoAPI(string(external_ip))
|
||||
if result != nil {
|
||||
fmt.Println("国家:", result.Data.Country)
|
||||
fmt.Println("地区:", result.Data.Area)
|
||||
fmt.Println("城市:", result.Data.City)
|
||||
fmt.Println("运营商:", result.Data.Isp)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("------Dividing Line------GetIntranetIp")
|
||||
|
||||
GetIntranetIp()
|
||||
|
||||
fmt.Println("------Dividing Line------")
|
||||
|
||||
ip_int := inet_aton(net.ParseIP(external_ip))
|
||||
fmt.Println("Convert IPv4 address to decimal number(base 10) :", ip_int)
|
||||
|
||||
ip_result := inet_ntoa(ip_int)
|
||||
fmt.Println("Convert decimal number(base 10) to IPv4 address:", ip_result)
|
||||
|
||||
fmt.Println("------Dividing Line------")
|
||||
|
||||
is_between := IpBetween(net.ParseIP("0.0.0.0"), net.ParseIP("255.255.255.255"), net.ParseIP(external_ip))
|
||||
fmt.Println("check result: ", is_between)
|
||||
|
||||
fmt.Println("------Dividing Line------external_ip")
|
||||
is_public_ip := IsPublicIP(net.ParseIP(external_ip))
|
||||
fmt.Println("It is public ip: ", is_public_ip)
|
||||
|
||||
is_public_ip = IsPublicIP(net.ParseIP("169.254.85.131"))
|
||||
fmt.Println("It is public ip: ", is_public_ip)
|
||||
|
||||
fmt.Println("------Dividing Line------GetPulicIP")
|
||||
fmt.Println(GetPulicIP())
|
||||
}
|
71
common/runner.go
Normal file
71
common/runner.go
Normal file
@ -0,0 +1,71 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrTimeOut = errors.New("执行者执行超时")
|
||||
var ErrInterrupt = errors.New("执行者被中断")
|
||||
|
||||
//一个执行者,可以执行任何任务,但是这些任务是限制完成的,
|
||||
//该执行者可以通过发送终止信号终止它
|
||||
type Runner struct {
|
||||
tasks []func(int) //要执行的任务
|
||||
complete chan error //用于通知任务全部完成
|
||||
timeout <-chan time.Time //这些任务在多久内完成
|
||||
interrupt chan os.Signal //可以控制强制终止的信号
|
||||
}
|
||||
|
||||
func New(tm time.Duration) *Runner {
|
||||
return &Runner{
|
||||
complete: make(chan error),
|
||||
timeout: time.After(tm),
|
||||
interrupt: make(chan os.Signal, 1),
|
||||
}
|
||||
}
|
||||
|
||||
//将需要执行的任务,添加到Runner里
|
||||
func (r *Runner) Add(tasks ...func(int)) {
|
||||
r.tasks = append(r.tasks, tasks...)
|
||||
}
|
||||
|
||||
//执行任务,执行的过程中接收到中断信号时,返回中断错误
|
||||
//如果任务全部执行完,还没有接收到中断信号,则返回nil
|
||||
func (r *Runner) run() error {
|
||||
for id, task := range r.tasks {
|
||||
if r.isInterrupt() {
|
||||
return ErrInterrupt
|
||||
}
|
||||
task(id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//检查是否接收到了中断信号
|
||||
func (r *Runner) isInterrupt() bool {
|
||||
select {
|
||||
case <-r.interrupt:
|
||||
signal.Stop(r.interrupt)
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
//开始执行所有任务,并且监视通道事件
|
||||
func (r *Runner) Start() error {
|
||||
//希望接收哪些系统信号
|
||||
signal.Notify(r.interrupt, os.Interrupt)
|
||||
go func() {
|
||||
r.complete <- r.run()
|
||||
}()
|
||||
select {
|
||||
case err := <-r.complete:
|
||||
return err
|
||||
case <-r.timeout:
|
||||
return ErrTimeOut
|
||||
}
|
||||
}
|
33
common/runner_test.go
Normal file
33
common/runner_test.go
Normal file
@ -0,0 +1,33 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRunner(t *testing.T) {
|
||||
|
||||
log.Println("...开始执行任务...")
|
||||
timeout := 3 * time.Second
|
||||
r := New(timeout)
|
||||
r.Add(createTask(), createTask(), createTask())
|
||||
if err := r.Start(); err != nil {
|
||||
switch err {
|
||||
case ErrTimeOut:
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
case ErrInterrupt:
|
||||
log.Println(err)
|
||||
os.Exit(2)
|
||||
}
|
||||
}
|
||||
log.Println("...任务执行结束...")
|
||||
}
|
||||
func createTask() func(int) {
|
||||
return func(id int) {
|
||||
log.Printf("正在执行任务%d", id)
|
||||
time.Sleep(time.Duration(id) * time.Second)
|
||||
}
|
||||
}
|
@ -13,7 +13,27 @@ func main() {
|
||||
dest := flag.String("dest", "/Users/ehlxr/ehlxr/blog/resume/data.json", "destination file path")
|
||||
flag.Parse()
|
||||
|
||||
f, err := os.Open(*source)
|
||||
fmt.Printf("is these right? (n/no cancel)\n source file path: %s \n destination file path: %s\n", *source, *dest)
|
||||
var in string
|
||||
fmt.Scanln(&in)
|
||||
if in == "no" || in == "n" {
|
||||
fmt.Println("bye!")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
m := make(map[string]interface{})
|
||||
m["show"] = "1"
|
||||
m["content"] = string(readFile(*source))
|
||||
j, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
writeFile(*dest, j)
|
||||
fmt.Println("Done !")
|
||||
}
|
||||
|
||||
func readFile(path string) []byte {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -23,20 +43,11 @@ func main() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
m := make(map[string]interface{})
|
||||
m["show"] = "1"
|
||||
m["content"] = string(fd)
|
||||
j, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return fd
|
||||
}
|
||||
|
||||
fmt.Println(string(j))
|
||||
writeFile(*dest, j)
|
||||
}
|
||||
|
||||
func writeFile(fn string, b []byte) {
|
||||
file, err := os.OpenFile(fn, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0777)
|
||||
func writeFile(path string, b []byte) {
|
||||
file, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0777)
|
||||
defer file.Close()
|
||||
|
||||
if err != nil {
|
Loading…
Reference in New Issue
Block a user