Skip to content

Commit

Permalink
修复 modprobe 报错
Browse files Browse the repository at this point in the history
  • Loading branch information
bjdgyc committed Dec 25, 2023
1 parent 64404ea commit d3f16eb
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 55 deletions.
5 changes: 4 additions & 1 deletion build_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ echo $ver
#docker login -u bjdgyc

#docker build -t bjdgyc/anylink .
docker build -t bjdgyc/anylink -f docker/Dockerfile .

docker build -t bjdgyc/anylink --build-arg GitCommitId=$(git rev-parse HEAD) -f docker/Dockerfile .

docker tag bjdgyc/anylink:latest bjdgyc/anylink:$ver

exit 0

docker push bjdgyc/anylink:$ver
docker push bjdgyc/anylink:latest

60 changes: 31 additions & 29 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,39 @@
## Donator

> 感谢以下同学的打赏,AnyLink 有你更美好!
>
>
> 需要展示主页的同学,可以在QQ群 直接联系我添加。
| 昵称 | 主页 |
|---------| ---------------------------- |
| 代码 oo8 | |
| 甘磊 | https://github.com/ganlei333 |
| Oo@ | https://github.com/chooop |
| 虚极静笃 | |
| 请喝可乐 | |
| 加油加油 | |
| 李建 | |
| lanbin | |
| 乐在东途 | |
| 孤鸿 | |
| 刘国华 | |
| 改名好无聊 | |
| 全能互联网专家 | |
| JCM | |
| Eh... | |
|| |
| 刘国华 | |
| 忧郁的豚骨拉面 | |
| 张小旋当爹地 | |
| 对方正在输入 | |
| Ronny | |
| 奔跑的少年 | |
| ZBW | |
| 悲鸣 | |
| 谢谢 | |
| 云思科技 | |
| 昵称 | 主页 / 留言 |
|-----------|------------------------------|
| 代码 oo8 | |
| 甘磊 | https://github.com/ganlei333 |
| Oo@ | https://github.com/chooop |
| 虚极静笃 | |
| 请喝可乐 | |
| 加油加油 | |
| 李建 | |
| lanbin | |
| 乐在东途 | |
| 孤鸿 | |
| 刘国华 | |
| 改名好无聊 | |
| 全能互联网专家 | |
| JCM | |
| Eh... | |
|| |
| 刘国华 | |
| 忧郁的豚骨拉面 | |
| 张小旋当爹地 | |
| 对方正在输入 | |
| Ronny | |
| 奔跑的少年 | |
| ZBW | |
| 悲鸣 | |
| 谢谢 | |
| 云思科技 | |
| 哆啦A伟(张佳伟) | |
| nobody | 开源不易,感谢分享 |



Expand Down
34 changes: 20 additions & 14 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,42 +1,48 @@
#node:16-bullseye
#golang:1.20-bullseye
#debian:bullseye-slim

# web
FROM node:16.17.1-alpine3.15 as builder_node
FROM node:16-alpine3.18 as builder_node
WORKDIR /web
COPY ./web /web
RUN yarn install \
&& yarn run build \
&& ls /web/ui


# server
FROM golang:1.19-alpine as builder_golang
FROM golang:1.20-alpine3.18 as builder_golang
#TODO 本地打包时使用镜像
ENV GOPROXY=https://goproxy.io
ENV GOPROXY=https://goproxy.cn
ENV GOOS=linux
ARG GitCommitId="gitCommitId"

WORKDIR /anylink
COPY . /anylink
COPY --from=builder_node /web/ui /anylink/server/ui
COPY server /anylink
COPY --from=builder_node /web/ui /anylink/ui

#TODO 本地打包时使用镜像
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
RUN apk add --no-cache git gcc musl-dev
RUN cd /anylink/server;go mod tidy;go build -o anylink -ldflags "-X main.CommitId=$(git rev-parse HEAD)" \
&& /anylink/server/anylink tool -v
RUN apk add gcc musl-dev
RUN cd /anylink;go mod tidy;go build -o anylink -ldflags "-s -w -X main.CommitId=${GitCommitId}" \
&& /anylink/anylink tool -v


# anylink
FROM alpine
FROM alpine:3.18
LABEL maintainer="github.com/bjdgyc"

#ENV IPV4_CIDR="192.168.10.0/24"
ENV ANYLINK_IN_CONTAINER=true

WORKDIR /app
COPY --from=builder_golang /anylink/server/anylink /app/
COPY --from=builder_golang /anylink/anylink /app/
COPY docker/docker_entrypoint.sh /app/

#COPY ./server/bridge-init.sh /app/
COPY ./server/bridge-init.sh /app/
COPY ./server/conf /app/conf
COPY ./LICENSE /app/LICENSE
COPY ./home /app/home


#TODO 本地打包时使用镜像
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
RUN apk add --no-cache bash iptables \
Expand Down
2 changes: 1 addition & 1 deletion server/base/app_ver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package base
const (
APP_NAME = "AnyLink"
// app版本号
APP_VER = "0.9.4"
APP_VER = "0.10.1"
)
65 changes: 65 additions & 0 deletions server/base/mod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package base

import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"strings"
)

const (
procModulesPath = "/proc/modules"
inContainerKey = "ANYLINK_IN_CONTAINER"
)

var (
inContainer = false
modMap = map[string]struct{}{}
)

func initMod() {
container := os.Getenv(inContainerKey)
if container == "true" {
inContainer = true
}
log.Println("inContainer", inContainer)

file, err := os.Open(procModulesPath)
if err != nil {
err = fmt.Errorf("[ERROR] Problem with open file: %s", err)
panic(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
splited := strings.Split(scanner.Text(), " ")
if len(splited[0]) > 0 {
modMap[splited[0]] = struct{}{}
}
}
}

func CheckModOrLoad(mod string) {
log.Println("CheckModOrLoad", mod)

if _, ok := modMap[mod]; ok {
return
}

if inContainer {
err := fmt.Errorf("Linux modules %s is not loaded, please run `modprobe %s`", mod, mod)
panic(err)
}

cmdstr := fmt.Sprintln("modprobe", mod)

cmd := exec.Command("sh", "-c", cmdstr)
b, err := cmd.CombinedOutput()
if err != nil {
log.Println(string(b))
panic(err)
}
}
1 change: 1 addition & 0 deletions server/base/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ func Start() {
execute()
initCfg()
initLog()
initMod()
}

func Test() {
Expand Down
13 changes: 5 additions & 8 deletions server/handler/link_tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ func checkTun() {
defer ifce.Close()

// 测试ip命令
cmdstr0 := fmt.Sprintln("modprobe -i tun")
base.CheckModOrLoad("tun")

cmdstr1 := fmt.Sprintf("ip link set dev %s up mtu %s multicast off", ifce.Name(), "1399")
err = execCmd([]string{cmdstr0, cmdstr1})
err = execCmd([]string{cmdstr1})
if err != nil {
base.Fatal("testTun err: ", err)
}
Expand All @@ -41,12 +42,8 @@ func checkTun() {
}

// 修复 rockyos nat 不生效
cmdstr0 := fmt.Sprintln("modprobe -i iptable_filter")
cmdstr1 := fmt.Sprintf("modprobe -i iptable_nat")
err = execCmd([]string{cmdstr0, cmdstr1})
if err != nil {
base.Fatal("testTun err: ", err)
}
base.CheckModOrLoad("iptable_filter")
base.CheckModOrLoad("iptable_nat")

natRule := []string{"-s", base.Cfg.Ipv4CIDR, "-o", base.Cfg.Ipv4Master, "-j", "MASQUERADE"}
forwardRule := []string{"-j", "ACCEPT"}
Expand Down
5 changes: 3 additions & 2 deletions server/handler/link_vtap.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ func checkMacvtap() {

ifName := "anylinkMacvtap"
// 加载 macvtap
cmdstr0 := fmt.Sprintln("modprobe -i macvtap")
base.CheckModOrLoad("macvtap")

// 开启主网卡混杂模式
cmdstr1 := fmt.Sprintf("ip link set dev %s promisc on", base.Cfg.Ipv4Master)
// 测试 macvtap 功能
cmdstr2 := fmt.Sprintf("ip link add link %s name %s type macvtap mode bridge", base.Cfg.Ipv4Master, ifName)
cmdstr3 := fmt.Sprintf("ip link del %s", ifName)
err := execCmd([]string{cmdstr0, cmdstr1, cmdstr2, cmdstr3})
err := execCmd([]string{cmdstr1, cmdstr2, cmdstr3})
if err != nil {
base.Fatal(err)
}
Expand Down

0 comments on commit d3f16eb

Please sign in to comment.