From b313b8ee04fef95379ac010a5674b0a5dd6f0258 Mon Sep 17 00:00:00 2001
From: liuxiaolong <736321739@qq.com>
Date: 星期一, 22 七月 2019 14:03:57 +0800
Subject: [PATCH] add gateway
---
controllers/syssetcont.go | 6 +++
config/dev.yaml | 1
extend/util/util.go | 77 ++++++++++++++++++++++++++++++++++++++
extend/config/config.go | 1
4 files changed, 85 insertions(+), 0 deletions(-)
diff --git a/config/dev.yaml b/config/dev.yaml
index c8e48e5..cc83bd5 100644
--- a/config/dev.yaml
+++ b/config/dev.yaml
@@ -4,6 +4,7 @@
jwtExpire: 24
url: http://127.0.0.1:8080
imageUrl: http://192.168.1.203:6080
+ networkAdapter: enp59s0f1
weedfs:
ip: 192.168.1.203
uploadport: 6333
diff --git a/controllers/syssetcont.go b/controllers/syssetcont.go
index fb4aa71..ef1888c 100644
--- a/controllers/syssetcont.go
+++ b/controllers/syssetcont.go
@@ -4,6 +4,7 @@
"basic.com/dbapi.git"
"github.com/gin-gonic/gin"
"webserver/extend/code"
+ "webserver/extend/config"
"webserver/extend/util"
)
@@ -147,6 +148,11 @@
}else {
resData["server_id"] = sysconf.GetServerId()
resData["server_name"] = sysconf.GetServerName()
+ ipv4, mask, _ := util.GetLocalIP(config.Server.NetworkAdapter)
+ gateway, _ := util.GetDefaultRoute(config.Server.NetworkAdapter)
+ resData["ip"] = ipv4
+ resData["subMask"] = mask
+ resData["gateway"] = gateway
// 瀛樺湪
util.ResponseFormat(c, code.Success, resData)
}
diff --git a/extend/config/config.go b/extend/config/config.go
index 4b182ee..3de0097 100644
--- a/extend/config/config.go
+++ b/extend/config/config.go
@@ -12,6 +12,7 @@
JwtExpire string `mapstructure: "jwtExpire"`
Url string `mapstructure: "url"`
ImageUrl string `mapstructure: "imageUrl"`
+ NetworkAdapter string `mapstructure: "networkAdapter"`
}
var Server = &server{}
diff --git a/extend/util/util.go b/extend/util/util.go
index 77cd755..cc2dd98 100644
--- a/extend/util/util.go
+++ b/extend/util/util.go
@@ -2,11 +2,16 @@
import (
"basic.com/pubsub/protomsg.git"
+ "bytes"
"encoding/json"
+ "errors"
"github.com/gin-gonic/gin"
"github.com/golang/glog"
"gocv.io/x/gocv"
"image"
+ "net"
+ "os/exec"
+ "strconv"
"webserver/extend/code"
"crypto/rand"
@@ -112,4 +117,76 @@
y1_new = int(i.Height)
}
return
+}
+
+
+// 鑾峰彇鏈満缃戝崱IP
+func GetLocalIP(networkName string) (ipv4 string,mask string, err error) {
+ interfaces, err := net.Interfaces()
+ if err != nil {
+ return "","", err
+ }
+
+ for _, i := range interfaces {
+ byName, err := net.InterfaceByName(i.Name)
+ if err != nil {
+ return "","", err
+ }
+ addresses, err := byName.Addrs()
+ for _, v := range addresses {
+ if ipnet, ok:=v.(*net.IPNet);ok && !ipnet.IP.IsLoopback(){
+ if ipnet.IP.To4() !=nil{
+ if byName.Name == networkName{
+ maskStr := ipnet.Mask.String()
+ mask64, _ := strconv.ParseUint(maskStr, 16, 32)
+ return ipnet.IP.String(),IpIntToString(int(mask64)),nil
+ }
+ }
+ }
+ }
+ }
+ return "","", errors.New("ipv4 not found")
+}
+
+func GetDefaultRoute(networkName string)(route string,err error){
+ cmdStr := fmt.Sprintf("route | grep -P \"^default.*%s$\" | awk '{print $2}'",networkName)
+ cmd := exec.Command("/bin/sh","-c",cmdStr)
+ b, err := cmd.Output()
+ if err!=nil{
+ return "",err
+ }
+ return string(b),nil
+
+}
+
+func StringIpToInt(ipstring string) int {
+ ipSegs := strings.Split(ipstring, ".")
+ var ipInt int = 0
+ var pos uint = 24
+ for _, ipSeg := range ipSegs {
+ tempInt, _ := strconv.Atoi(ipSeg)
+ tempInt = tempInt << pos
+ ipInt = ipInt | tempInt
+ pos -= 8
+ }
+ return ipInt
+}
+
+func IpIntToString(ipInt int) string{
+ ipSegs := make([]string, 4)
+ var len int = len(ipSegs)
+ buffer := bytes.NewBufferString("")
+ for i := 0; i < len; i++ {
+ tempInt := ipInt & 0xFF
+ ipSegs[len-i-1] = strconv.Itoa(tempInt)
+
+ ipInt = ipInt >> 8
+ }
+ for i := 0; i < len; i++ {
+ buffer.WriteString(ipSegs[i])
+ if i < len-1 {
+ buffer.WriteString(".")
+ }
+ }
+ return buffer.String()
}
\ No newline at end of file
--
Gitblit v1.8.0