增加了CIDR判断和返回数据的debug信息

This commit is contained in:
bjd 2021-01-12 17:30:31 +08:00
parent a72fc63c06
commit 3ebb669558
5 changed files with 39 additions and 39 deletions

View File

@ -2,5 +2,5 @@ package base
const ( const (
APP_NAME = "AnyLink" APP_NAME = "AnyLink"
APP_VER = "0.0.7" APP_VER = "0.0.8"
) )

View File

@ -22,7 +22,7 @@ admin_pass = "$2a$10$UQ7C.EoPifDeJh6d8.31TeSPQU7hM/NOM2nixmBucJpAuXDQNqNke"
jwt_secret = "" jwt_secret = ""
#vpn服务对外地址 #vpn服务对外地址,影响开通邮件二维码
link_addr = "vpn.xx.com" link_addr = "vpn.xx.com"
#前台服务监听地址 #前台服务监听地址

View File

@ -4,7 +4,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"net" "net"
"strings"
"time" "time"
"github.com/bjdgyc/anylink/base" "github.com/bjdgyc/anylink/base"
@ -20,11 +19,12 @@ type GroupLinkAcl struct {
Action string `json:"action"` // allow、deny Action string `json:"action"` // allow、deny
Val string `json:"val"` Val string `json:"val"`
Port uint8 `json:"port"` Port uint8 `json:"port"`
IpNet *net.IPNet `json:"-"` IpNet *net.IPNet `json:"ip_net"`
} }
type ValData struct { type ValData struct {
Val string `json:"val"` Val string `json:"val"`
IpMask string `json:"ip_mask"`
} }
type Group struct { type Group struct {
@ -70,15 +70,18 @@ func SetGroup(g *Group) error {
} }
} }
if len(clientDns) == 0 { if len(clientDns) == 0 {
return errors.New("DNS错误") return errors.New("DNS 错误")
} }
g.ClientDns = clientDns g.ClientDns = clientDns
routeInclude := []ValData{} routeInclude := []ValData{}
for _, v := range g.RouteInclude { for _, v := range g.RouteInclude {
if v.Val != "" { if v.Val != "" {
v1, _ := parseIpNet(v.Val) ipMask, _, err := parseIpNet(v.Val)
vn := ValData{Val: v1} if err != nil {
return errors.New("RouteInclude 错误" + err.Error())
}
vn := ValData{Val: v.Val, IpMask: ipMask}
routeInclude = append(routeInclude, vn) routeInclude = append(routeInclude, vn)
} }
} }
@ -86,8 +89,11 @@ func SetGroup(g *Group) error {
routeExclude := []ValData{} routeExclude := []ValData{}
for _, v := range g.RouteExclude { for _, v := range g.RouteExclude {
if v.Val != "" { if v.Val != "" {
v1, _ := parseIpNet(v.Val) ipMask, _, err := parseIpNet(v.Val)
vn := ValData{Val: v1} if err != nil {
return errors.New("RouteExclude 错误" + err.Error())
}
vn := ValData{Val: v.Val, IpMask: ipMask}
routeExclude = append(routeExclude, vn) routeExclude = append(routeExclude, vn)
} }
} }
@ -96,13 +102,13 @@ func SetGroup(g *Group) error {
linkAcl := []GroupLinkAcl{} linkAcl := []GroupLinkAcl{}
for _, v := range g.LinkAcl { for _, v := range g.LinkAcl {
if v.Val != "" { if v.Val != "" {
v1, v2 := parseIpNet(v.Val) _, ipNet, err := parseIpNet(v.Val)
if v2 != nil { if err != nil {
vn := v return errors.New("GroupLinkAcl 错误" + err.Error())
vn.Val = v1
vn.IpNet = v2
linkAcl = append(linkAcl, vn)
} }
vn := v
vn.IpNet = ipNet
linkAcl = append(linkAcl, vn)
} }
} }
g.LinkAcl = linkAcl g.LinkAcl = linkAcl
@ -113,24 +119,14 @@ func SetGroup(g *Group) error {
return err return err
} }
func parseIpNet(s string) (string, *net.IPNet) { func parseIpNet(s string) (string, *net.IPNet, error) {
ips := strings.Split(s, "/") ip, ipNet, err := net.ParseCIDR(s)
if len(ips) != 2 { if err != nil {
return "", nil return "", nil, err
}
ip := net.ParseIP(ips[0])
mask := net.ParseIP(ips[1])
if strings.Contains(ips[0], ".") {
ip = ip.To4()
mask = mask.To4()
} }
ipmask := net.IPMask(mask) mask := net.IP(ipNet.Mask)
ip0 := ip.Mask(ipmask) ipMask := fmt.Sprintf("%s/%s", ip, mask)
ipNetS := fmt.Sprintf("%s/%s", ip0, mask) return ipMask, ipNet, nil
ipNet := &net.IPNet{IP: ip0, Mask: ipmask}
return ipNetS, ipNet
} }

View File

@ -49,12 +49,12 @@ func LinkCstp(conn net.Conn, cSess *sessdata.ConnSession) {
switch hdata[6] { switch hdata[6] {
case 0x07: // KEEPALIVE case 0x07: // KEEPALIVE
// do nothing // do nothing
base.Debug("recv keepalive", cSess.IpAddr) // base.Debug("recv keepalive", cSess.IpAddr)
case 0x05: // DISCONNECT case 0x05: // DISCONNECT
base.Debug("DISCONNECT", cSess.IpAddr) base.Debug("DISCONNECT", cSess.IpAddr)
return return
case 0x03: // DPD-REQ case 0x03: // DPD-REQ
base.Debug("recv DPD-REQ", cSess.IpAddr) // base.Debug("recv DPD-REQ", cSess.IpAddr)
if payloadOut(cSess, sessdata.LTypeIPData, 0x04, nil) { if payloadOut(cSess, sessdata.LTypeIPData, 0x04, nil) {
return return
} }

View File

@ -1,6 +1,7 @@
package handler package handler
import ( import (
"bytes"
"fmt" "fmt"
"log" "log"
"net" "net"
@ -86,11 +87,11 @@ func LinkTunnel(w http.ResponseWriter, r *http.Request) {
} }
// 允许的路由 // 允许的路由
for _, v := range cSess.Group.RouteInclude { for _, v := range cSess.Group.RouteInclude {
w.Header().Add("X-CSTP-Split-Include", v.Val) w.Header().Add("X-CSTP-Split-Include", v.IpMask)
} }
// 不允许的路由 // 不允许的路由
for _, v := range cSess.Group.RouteExclude { for _, v := range cSess.Group.RouteExclude {
w.Header().Add("X-CSTP-Split-Exclude", v.Val) w.Header().Add("X-CSTP-Split-Exclude", v.IpMask)
} }
w.Header().Set("X-CSTP-Lease-Duration", fmt.Sprintf("%d", base.Cfg.IpLease)) // ip地址租期 w.Header().Set("X-CSTP-Lease-Duration", fmt.Sprintf("%d", base.Cfg.IpLease)) // ip地址租期
@ -130,8 +131,11 @@ func LinkTunnel(w http.ResponseWriter, r *http.Request) {
// w.Header().Set("X-CSTP-Post-Auth-XML", ``) // w.Header().Set("X-CSTP-Post-Auth-XML", ``)
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
// h := w.Header().Clone() h := w.Header().Clone()
// h.Write(os.Stdout) headers := make([]byte, 0)
buf := bytes.NewBuffer(headers)
h.Write(buf)
base.Debug(string(buf.Bytes()))
hj := w.(http.Hijacker) hj := w.(http.Hijacker)
conn, _, err := hj.Hijack() conn, _, err := hj.Hijack()