Skip to content

Commit

Permalink
Merge pull request #304 from bjdgyc/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
bjdgyc authored Mar 19, 2024
2 parents 8ea158a + 268e9c4 commit bdc8e26
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 79 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,27 @@ sudo ./anylink
- [x] 兼容 AnyConnect
- [x] 兼容 OpenConnect
- [x] 基于 tun 设备的 nat 访问模式
- [x] 基于 tap 设备的桥接访问模式
- [x] 基于 tun 设备的桥接访问模式
- [x] 基于 macvtap 设备的桥接访问模式
- [x] 支持 [proxy protocol v1&v2](http://www.haproxy.org/download/2.2/doc/proxy-protocol.txt) 协议
- [x] 用户组支持
- [x] 用户组策略支持
- [x] 多用户支持
- [x] 用户策略支持
- [x] TOTP 令牌支持
- [x] TOTP 令牌开关
- [x] 流量速率限制
- [x] 后台管理界面
- [x] 访问权限管理
- [x] 用户活动审计功能
- [x] IP 访问审计功能
- [x] 域名动态拆分隧道(域名路由功能)
- [x] radius认证支持
- [x] LDAP认证支持
- [x] 空闲链接超时自动断开
- [x] 流量压缩功能
- [x] 出口 IP 自动放行
- [x] 支持多服务的配置区分
- [ ] 基于 ipvtap 设备的桥接访问模式

## Config
Expand Down
65 changes: 6 additions & 59 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,74 +1,21 @@
#!/bin/bash

#github action release.sh

set -x
function RETVAL() {
rt=$1
if [ $rt != 0 ]; then
echo $rt
exit 1
fi
}

#当前目录
cpath=$(pwd)

ver=$(cat version)
echo $ver

echo "copy二进制文件"
cd $cpath/server
# -tags osusergo,netgo,sqlite_omit_load_extension
flags="-trimpath"
ldflags="-s -w -extldflags '-static' -X main.appVer=$ver -X main.commitId=$(git rev-parse HEAD) -X main.buildDate=$(date --iso-8601=seconds)"
#github action
gopath=/go

dockercmd=$(
cat <<EOF
sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
apk add gcc g++ musl musl-dev tzdata
export GOPROXY=https://goproxy.cn
go mod tidy
echo "build:"
export CGO_ENABLED=1
go build -v -o anylink_amd64 $flags -ldflags "$ldflags"
./anylink_amd64 -v
EOF
)
#前端编译 仅需要执行一次
bash ./build_web.sh

#使用 musl-dev 编译
docker run -q --rm -v $PWD:/app -v $gopath:/go -w /app --platform=linux/amd64 \
golang:1.20-alpine3.19 sh -c "$dockercmd"

exit 0

#arm64编译
docker run -q --rm -v $PWD:/app -v $gopath:/go -w /app --platform=linux/arm64 \
golang:1.20-alpine3.19 go build -o anylink_arm64 $flags -ldflags "$ldflags"
./anylink_arm64 -v

exit 0
cd $cpath/server

cd $cpath
go build -v -o anylink

echo "整理部署文件"
deploy="anylink-deploy"
rm -rf $deploy ${deploy}.tar.gz
mkdir $deploy
mkdir $deploy/log
./anylink -v

cp -r server/anylink $deploy
cp -r server/bridge-init.sh $deploy
cp -r server/conf $deploy

cp -r systemd $deploy
cp -r LICENSE $deploy
cp -r home $deploy
echo "anylink 编译完成,目录: $cpath/server/anylink"

tar zcvf ${deploy}.tar.gz $deploy

#注意使用root权限运行
#cd anylink-deploy
#sudo ./anylink --conf="conf/server.toml"
72 changes: 72 additions & 0 deletions build_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/bash

#github action release.sh

set -x
function RETVAL() {
rt=$1
if [ $rt != 0 ]; then
echo $rt
exit 1
fi
}

#当前目录
cpath=$(pwd)

ver=$(cat version)
echo $ver

#前端编译 仅需要执行一次
#bash ./build_web.sh

echo "copy二进制文件"

# -tags osusergo,netgo,sqlite_omit_load_extension
flags="-trimpath"
ldflags="-s -w -extldflags '-static' -X main.appVer=$ver -X main.commitId=$(git rev-parse HEAD) -X main.buildDate=$(date --iso-8601=seconds)"
#github action
gopath=/go

dockercmd=$(
cat <<EOF
sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
apk add gcc g++ musl musl-dev tzdata
export GOPROXY=https://goproxy.cn
go mod tidy
echo "build:"
rm anylink
export CGO_ENABLED=1
go build -v -o anylink $flags -ldflags "$ldflags"
./anylink -v
EOF
)

#使用 musl-dev 编译
docker run -q --rm -v $PWD/server:/app -v $gopath:/go -w /app --platform=linux/amd64 \
golang:1.20-alpine3.19 sh -c "$dockercmd"

#arm64编译
#docker run -q --rm -v $PWD/server:/app -v $gopath:/go -w /app --platform=linux/arm64 \
# golang:1.20-alpine3.19 go build -o anylink_arm64 $flags -ldflags "$ldflags"
#exit 0

#cd $cpath

echo "整理部署文件"
rm -rf anylink-deploy anylink-deploy.tar.gz
mkdir anylink-deploy
mkdir anylink-deploy/log

cp -r server/anylink anylink-deploy
cp -r server/conf anylink-deploy

cp -r index_template anylink-deploy
cp -r deploy anylink-deploy
cp -r LICENSE anylink-deploy

tar zcvf anylink-deploy.tar.gz anylink-deploy

#注意使用root权限运行
#cd anylink-deploy
#sudo ./anylink --conf="conf/server.toml"
4 changes: 2 additions & 2 deletions server/base/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type config struct {
var configs = []config{
{Typ: cfgStr, Name: "conf", Usage: "config file", ValStr: "./conf/server.toml", Short: "c"},
{Typ: cfgStr, Name: "profile", Usage: "profile.xml file", ValStr: "./conf/profile.xml"},
{Typ: cfgStr, Name: "profile_name", Usage: "profile name(用于区分不同网站的配置)", ValStr: "anylink"},
{Typ: cfgStr, Name: "profile_name", Usage: "profile name(用于区分不同服务端的配置)", ValStr: "anylink"},
{Typ: cfgStr, Name: "server_addr", Usage: "TCP服务监听地址(任意端口)", ValStr: ":443"},
{Typ: cfgBool, Name: "server_dtls", Usage: "开启DTLS", ValBool: false},
{Typ: cfgStr, Name: "server_dtls_addr", Usage: "DTLS监听地址(任意端口)", ValStr: ":443"},
Expand Down Expand Up @@ -59,7 +59,7 @@ var configs = []config{
{Typ: cfgInt, Name: "mobile_keepalive", Usage: "移动端keepalive接检测时间(秒)", ValInt: 40},
{Typ: cfgInt, Name: "mobile_dpd", Usage: "移动端死链接检测时间(秒)", ValInt: 60},
{Typ: cfgInt, Name: "mtu", Usage: "最大传输单元MTU", ValInt: 1460},
{Typ: cfgInt, Name: "idle_timeout", Usage: "空闲链接超时时间(秒)-超时后断开链接,0关闭此功能", ValInt: 1800},
{Typ: cfgInt, Name: "idle_timeout", Usage: "空闲链接超时时间(秒)-超时后断开链接,0关闭此功能", ValInt: 0},
{Typ: cfgInt, Name: "session_timeout", Usage: "session过期时间(秒)-用于断线重连,0永不过期", ValInt: 3600},
// {Typ: cfgInt, Name: "auth_timeout", Usage: "auth_timeout", ValInt: 0},
{Typ: cfgInt, Name: "audit_interval", Usage: "审计去重间隔(秒),-1关闭", ValInt: 600},
Expand Down
6 changes: 3 additions & 3 deletions server/conf/server-sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cert_file = "./conf/vpn_cert.pem"
cert_key = "./conf/vpn_cert.key"
files_path = "./conf/files"
profile = "./conf/profile.xml"
#profile name(用于区分不同网站的配置)
#profile name(用于区分不同服务端的配置)
#客户端存放位置 C:\ProgramData\Cisco\Cisco Secure Client\VPN\Profile
profile_name = "anylink"
#日志目录,为空写入标准输出
Expand Down Expand Up @@ -82,10 +82,10 @@ default_domain = "example.com"
#default_domain = "example.com abc.example.com"

#空闲链接超时时间(秒)-超时后断开链接,0关闭此功能
idle_timeout = 1800
idle_timeout = 0
#session过期时间,用于断线重连,0永不过期
session_timeout = 3600
auth_timeout = 0
#auth_timeout = 0
audit_interval = 600

show_sql = false
Expand Down
5 changes: 3 additions & 2 deletions server/handler/link_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ func LinkAuth(w http.ResponseWriter, r *http.Request) {
cr := ClientRequest{}
err = xml.Unmarshal(body, &cr)
if err != nil {
base.Error(err)
w.WriteHeader(http.StatusBadRequest)
return
}
// fmt.Printf("%+v \n", cr)
base.Trace(fmt.Sprintf("%+v \n", cr))
// setCommonHeader(w)
if cr.Type == "logout" {
// 退出删除session信息
Expand Down Expand Up @@ -140,7 +141,7 @@ func LinkAuth(w http.ResponseWriter, r *http.Request) {
Banner: other.Banner, ProfileName: base.Cfg.ProfileName, ProfileHash: profileHash}
w.WriteHeader(http.StatusOK)
tplRequest(tpl_complete, w, rd)
base.Debug("login", cr.Auth.Username, userAgent)
base.Info("login", cr.Auth.Username, userAgent)
}

const (
Expand Down
8 changes: 4 additions & 4 deletions server/handler/link_cstp.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ func LinkCstp(conn net.Conn, bufRW *bufio.ReadWriter, cSess *sessdata.ConnSessio
// 设置超时限制
err = conn.SetReadDeadline(utils.NowSec().Add(dead))
if err != nil {
base.Error("SetDeadline: ", cSess.Username, err)
base.Error("SetDeadline: ", cSess.Username, cSess.IpAddr, err)
return
}
// hdata := make([]byte, BufferSize)
pl := getPayload()
n, err = bufRW.Read(pl.Data)
if err != nil {
base.Error("read hdata: ", cSess.Username, err)
base.Warn("read hdata: ", cSess.Username, cSess.IpAddr, err)
return
}

Expand All @@ -69,7 +69,7 @@ func LinkCstp(conn net.Conn, bufRW *bufio.ReadWriter, cSess *sessdata.ConnSessio
}
case 0x05: // DISCONNECT
cSess.UserLogoutCode = dbdata.UserLogoutClient
base.Debug("DISCONNECT", cSess.Username, cSess.IpAddr, conn.RemoteAddr(), n, string(pl.Data[9:n]))
base.Info("DISCONNECT", cSess.Username, cSess.IpAddr, conn.RemoteAddr(), n, string(pl.Data[9:n]))
sessdata.CloseSess(cSess.Sess.Token, dbdata.UserLogoutClient)
return
case 0x03: // DPD-REQ
Expand Down Expand Up @@ -176,7 +176,7 @@ func cstpWrite(conn net.Conn, bufRW *bufio.ReadWriter, cSess *sessdata.ConnSessi

n, err = conn.Write(pl.Data)
if err != nil {
base.Error("write err", cSess.Username, err)
base.Warn("write err", cSess.Username, cSess.IpAddr, err)
return
}

Expand Down
17 changes: 10 additions & 7 deletions server/handler/link_dtls.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
for {
err = conn.SetReadDeadline(utils.NowSec().Add(dead))
if err != nil {
base.Error("SetDeadline: ", cSess.Username, err)
base.Error("SetDeadline: ", cSess.Username, cSess.IpAddr, err)
return
}

pl := getPayload()
n, err = conn.Read(pl.Data)
if err != nil {
base.Error("read hdata: ", cSess.Username, err)
base.Warn("read hdata: ", cSess.Username, cSess.IpAddr, err)
return
}

Expand All @@ -59,10 +59,10 @@ func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
base.Trace("recv LinkDtls Keepalive", cSess.Username, cSess.IpAddr, conn.RemoteAddr())
case 0x05: // DISCONNECT
cSess.UserLogoutCode = dbdata.UserLogoutClient
base.Debug("DISCONNECT DTLS", cSess.Username, cSess.IpAddr, conn.RemoteAddr())
base.Info("DISCONNECT DTLS", cSess.Username, cSess.IpAddr, conn.RemoteAddr())
return
case 0x03: // DPD-REQ
base.Trace("recv LinkDtls DPD-REQ", cSess.Username, cSess.IpAddr, conn.RemoteAddr(), n, pl.Data[:n])
base.Trace("recv LinkDtls DPD-REQ", cSess.Username, cSess.IpAddr, conn.RemoteAddr(), n)
pl.PType = 0x04
// 从零开始 可以直接赋值
pl.Data = pl.Data[:n]
Expand Down Expand Up @@ -151,12 +151,15 @@ func dtlsWrite(conn net.Conn, dSess *sessdata.DtlsSession, cSess *sessdata.ConnS
}
} else {
// 设置头类型
// pl.Data = append(pl.Data[:0], pl.PType)
pl.Data[0] = pl.PType
if pl.PType == 0x04 {
pl.Data[0] = pl.PType
} else {
pl.Data = append(pl.Data[:0], pl.PType)
}
}
n, err := conn.Write(pl.Data)
if err != nil {
base.Error("write err", cSess.Username, err)
base.Warn("write err", cSess.Username, cSess.IpAddr, err)
return
}

Expand Down
1 change: 1 addition & 0 deletions server/handler/link_tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func checkTun() {
// 修复 rockyos nat 不生效
base.CheckModOrLoad("iptable_filter")
base.CheckModOrLoad("iptable_nat")
// base.CheckModOrLoad("xt_comment")

// 添加注释
natRule := []string{"-s", base.Cfg.Ipv4CIDR, "-o", base.Cfg.Ipv4Master, "-m", "comment",
Expand Down
2 changes: 1 addition & 1 deletion server/handler/link_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func LinkTunnel(w http.ResponseWriter, r *http.Request) {
dtlsPort = ss[1]
}

base.Debug(cSess.IpAddr, cSess.MacHw, sess.Username, mobile)
base.Info(sess.Username, cSess.IpAddr, cSess.MacHw, cSess.Client, mobile)

// 检测密码套件
dtlsCiphersuite := checkDtls12Ciphersuite(r.Header.Get("X-Dtls12-Ciphersuite"))
Expand Down

0 comments on commit bdc8e26

Please sign in to comment.