Skip to content

Commit 1a71b87

Browse files
committed
* add p2p multi-processes mode
* reduce compilation time * reduce the number of threads in process mode * improvements of building
1 parent 7bd656c commit 1a71b87

File tree

22 files changed

+4554
-35
lines changed

22 files changed

+4554
-35
lines changed

.github/workflows/container.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ jobs:
4040

4141
- name: package-server
4242
run: |-
43-
sudo apt-get update && sudo apt-get install make git gn ninja-build python3 python3-pip libgtk-3-dev gcc-aarch64-linux-gnu g++-aarch64-linux-gnu gcc-x86-64-linux-gnu g++-x86-64-linux-gnu -y
43+
sudo apt-get update && sudo apt-get install make git gn ninja-build python3 python3-pip libgtk-3-dev gcc-aarch64-linux-gnu g++-aarch64-linux-gnu gcc-x86-64-linux-gnu g++-x86-64-linux-gnu rustc -y
44+
rustup target add x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu
4445
GOARCH=amd64 make release_server && mv release/linux-amd64-server release/linux-x86_64-server
4546
TARGET=aarch64-linux-gnu GOOS=linux GOARCH=arm64 make release_server && mv release/linux-arm64-server release/linux-aarch64-server
4647
ls -al release
4748
4849
- name: package-client
4950
run: |-
50-
sudo apt-get update && sudo apt-get install make git gn ninja-build python3 python3-pip libgtk-3-dev gcc-aarch64-linux-gnu g++-aarch64-linux-gnu gcc-x86-64-linux-gnu g++-x86-64-linux-gnu -y
51+
sudo apt-get update && sudo apt-get install make git gn ninja-build python3 python3-pip libgtk-3-dev gcc-aarch64-linux-gnu g++-aarch64-linux-gnu gcc-x86-64-linux-gnu g++-x86-64-linux-gnu rustc -y
52+
rustup target add x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu
5153
GOARCH=amd64 make release_client && mv release/linux-amd64-client release/linux-x86_64-client
5254
TARGET=aarch64-linux-gnu GOOS=linux GOARCH=arm64 make release_client && mv release/linux-arm64-client release/linux-aarch64-client
5355
ls -al release

.gitmodules

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
url = https://git.mirror.iscas.ac.cn/ao-space/google-webrtc.git
1818
branch = main
1919
shallow = true
20+
ignore = dirty
2021
[submodule "dep/_msquic"]
2122
path = dep/_msquic
2223
url = https://github.com/microsoft/msquic
24+
ignore = dirty

Dockerfile

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,26 @@
1414

1515
FROM golang:1.20-bookworm
1616

17+
# 安装 gnu 编译链
18+
# 安装 msquic 依赖
19+
# 安装 nodejs 20
1720
RUN apt update && \
18-
apt install xz-utils bzip2 sudo lsb-release ninja-build generate-ninja file patch -y
21+
apt install -y xz-utils bzip2 sudo lsb-release ninja-build generate-ninja file patch \
22+
gcc-aarch64-linux-gnu g++-aarch64-linux-gnu gcc-x86-64-linux-gnu g++-x86-64-linux-gnu \
23+
cmake build-essential liblttng-ust-dev lttng-tools libssl-dev \
24+
ca-certificates curl gnupg
1925

20-
# 安装nodejs 20
21-
RUN apt-get install -y ca-certificates curl gnupg && \
22-
mkdir -p /etc/apt/keyrings && \
26+
RUN mkdir -p /etc/apt/keyrings && \
2327
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg && \
2428
NODE_MAJOR=20 && \
2529
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list && \
26-
apt-get update && \
27-
apt-get install nodejs -y
30+
apt update && \
31+
apt install nodejs -y
2832

29-
# 安装msquic依赖
30-
RUN apt-get install -y cmake build-essential liblttng-ust-dev lttng-tools libssl-dev
33+
# 安装 rust
34+
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \
35+
. "$HOME/.cargo/env" && \
36+
rustup target add x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu
3137

3238
# golang 切换国内源并且提前安装好依赖
3339
ENV GO111MODULE=on
@@ -37,9 +43,6 @@ RUN cd /go/src/github.com/isrc-cas/gt && \
3743
go mod download && \
3844
rm -rf /go/src/github.com/isrc-cas/gt
3945

40-
# 安装 gnu 编译链
41-
RUN apt install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu gcc-x86-64-linux-gnu g++-x86-64-linux-gnu
42-
4346
# 安装 google webrtc 依赖
4447
ADD ./dep/_google-webrtc/src/build /root/build
4548
RUN sed -i 's/egrep -q "i686|x86_64"/true/g' /root/build/install-build-deps.sh && \

Makefile

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ TARGET_OS=$(shell echo $(TARGET) | awk -F '-' '{print $$2}')
4040
ifeq ($(TARGET_OS), native)
4141
TARGET_OS=
4242
endif
43+
ifeq ($(TARGET_OS), linux)
44+
RUST_TARGET?=$(shell echo $(TARGET) | awk -F '-' '{print $$1 "-unknown-" $$2 "-" $$3}')
45+
endif
4346
TARGET_CPU=$(shell echo $(TARGET) | awk -F '-' '{print $$1}')
4447
ifeq ($(TARGET_CPU), native)
4548
TARGET_CPU=
@@ -136,8 +139,12 @@ set_safe_directories:
136139
docker_create_image: update_submodule
137140
docker images | grep -cim1 -E "^gtbuild\s+?v1" || docker build -t gtbuild:v1 .
138141

139-
docker_build_linux_amd64: docker_build_linux_amd64_server docker_build_linux_amd64_client
140-
docker_release_linux_amd64: docker_release_linux_amd64_server docker_release_linux_amd64_client
142+
docker_build_linux_amd64: docker_create_image
143+
$(eval MAKE_ENV=TARGET=x86_64-linux-gnu GOOS=linux GOARCH=amd64 STATIC_LINK=$(STATIC_LINK) RACE_CHECK=$(RACE_CHECK) WITH_OFFICIAL_WEBRTC=$(WITH_OFFICIAL_WEBRTC))
144+
docker run --rm -v $(PWD):/go/src/github.com/isrc-cas/gt -w /go/src/github.com/isrc-cas/gt gtbuild:v1 sh -c '$(MAKE_ENV) make build'
145+
docker_release_linux_amd64: docker_create_image
146+
$(eval MAKE_ENV=TARGET=x86_64-linux-gnu GOOS=linux GOARCH=amd64 STATIC_LINK=$(STATIC_LINK) RACE_CHECK=$(RACE_CHECK) WITH_OFFICIAL_WEBRTC=$(WITH_OFFICIAL_WEBRTC))
147+
docker run --rm -v $(PWD):/go/src/github.com/isrc-cas/gt -w /go/src/github.com/isrc-cas/gt gtbuild:v1 sh -c '$(MAKE_ENV) make release'
141148
docker_build_linux_amd64_client: docker_create_image
142149
$(eval MAKE_ENV=TARGET=x86_64-linux-gnu GOOS=linux GOARCH=amd64 STATIC_LINK=$(STATIC_LINK) RACE_CHECK=$(RACE_CHECK) WITH_OFFICIAL_WEBRTC=$(WITH_OFFICIAL_WEBRTC))
143150
docker run --rm -v $(PWD):/go/src/github.com/isrc-cas/gt -w /go/src/github.com/isrc-cas/gt gtbuild:v1 sh -c '$(MAKE_ENV) make build_client'
@@ -151,8 +158,12 @@ docker_release_linux_amd64_server: docker_create_image
151158
$(eval MAKE_ENV=TARGET=x86_64-linux-gnu GOOS=linux GOARCH=amd64 STATIC_LINK=$(STATIC_LINK) RACE_CHECK=$(RACE_CHECK) WITH_OFFICIAL_WEBRTC=$(WITH_OFFICIAL_WEBRTC))
152159
docker run --rm -v $(PWD):/go/src/github.com/isrc-cas/gt -w /go/src/github.com/isrc-cas/gt gtbuild:v1 sh -c '$(MAKE_ENV) make release_server'
153160

154-
docker_build_linux_arm64: docker_build_linux_arm64_server docker_build_linux_arm64_client
155-
docker_release_linux_arm64: docker_release_linux_arm64_server docker_release_linux_arm64_client
161+
docker_build_linux_arm64: docker_create_image
162+
$(eval MAKE_ENV=TARGET=aarch64-linux-gnu GOOS=linux GOARCH=arm64 STATIC_LINK=$(STATIC_LINK) RACE_CHECK=$(RACE_CHECK) WITH_OFFICIAL_WEBRTC=$(WITH_OFFICIAL_WEBRTC))
163+
docker run --rm -v $(PWD):/go/src/github.com/isrc-cas/gt -w /go/src/github.com/isrc-cas/gt gtbuild:v1 sh -c '$(MAKE_ENV) make build'
164+
docker_release_linux_arm64: docker_create_image
165+
$(eval MAKE_ENV=TARGET=aarch64-linux-gnu GOOS=linux GOARCH=arm64 STATIC_LINK=$(STATIC_LINK) RACE_CHECK=$(RACE_CHECK) WITH_OFFICIAL_WEBRTC=$(WITH_OFFICIAL_WEBRTC))
166+
docker run --rm -v $(PWD):/go/src/github.com/isrc-cas/gt -w /go/src/github.com/isrc-cas/gt gtbuild:v1 sh -c '$(MAKE_ENV) make release'
156167
docker_build_linux_arm64_client: docker_create_image
157168
$(eval MAKE_ENV=TARGET=aarch64-linux-gnu GOOS=linux GOARCH=arm64 STATIC_LINK=$(STATIC_LINK) RACE_CHECK=$(RACE_CHECK) WITH_OFFICIAL_WEBRTC=$(WITH_OFFICIAL_WEBRTC))
158169
docker run --rm -v $(PWD):/go/src/github.com/isrc-cas/gt -w /go/src/github.com/isrc-cas/gt gtbuild:v1 sh -c '$(MAKE_ENV) make build_client'
@@ -168,12 +179,14 @@ docker_release_linux_arm64_server: docker_create_image
168179

169180
build: build_server build_client
170181
release: release_server release_client
171-
build_client: $(SOURCES) Makefile compile_msquic compile_webrtc
182+
build_client: $(SOURCES) Makefile compile_msquic compile_webrtc compile_p2p
172183
$(eval CGO_CXXFLAGS+=-O0 -g -ggdb)
184+
$(eval CGO_LDFLAGS+=$(PWD)/dep/p2p/target/$(RUST_TARGET)/release/libp2p.a)
173185
$(eval NAME=$(GOOS)-$(GOARCH)-client)
174186
go build $(DEBUG_OPTIONS) -o build/$(NAME)$(EXE) ./cmd/client
175-
release_client: $(SOURCES) Makefile compile_msquic compile_webrtc release_web_client
187+
release_client: $(SOURCES) Makefile compile_msquic compile_webrtc compile_p2p release_web_client
176188
$(eval CGO_CXXFLAGS+=-O3)
189+
$(eval CGO_LDFLAGS+=$(PWD)/dep/p2p/target/$(RUST_TARGET)/release/libp2p.a)
177190
$(eval NAME=$(GOOS)-$(GOARCH)-client)
178191
go build $(RELEASE_OPTIONS) -o release/$(NAME)$(EXE) ./cmd/client
179192
build_server: $(SOURCES) Makefile compile_msquic compile_webrtc
@@ -203,7 +216,7 @@ duplicate_dist_server:
203216
duplicate_dist_client:
204217
cp -r $(FRONTEND_DIR)/dist $(CLIENT_FRONT_DIR)/dist
205218

206-
clean: clean_web
219+
clean: clean_web clean_p2p
207220
rm -rf build/* release/*
208221
rm -rf dep/_google-webrtc/src/out/*
209222

@@ -277,4 +290,14 @@ compile_msquic: check_msquic_dependencies update_submodule
277290
cmake -B./dep/_msquic/$(TARGET) -S./dep/_msquic -DQUIC_BUILD_SHARED=OFF -DCMAKE_TARGET_ARCHITECTURE=$(TARGET_CPU)
278291
make -C./dep/_msquic/$(TARGET) -j$(shell nproc)
279292
@renameSymbols=$$(objdump -t ./dep/_msquic/$(TARGET)/bin/Release/libmsquic.a | awk -v RS= '/_YB80VJ/{next}1' | grep -E 'g +(F|O) ' | grep -Evi ' (ms){0,1}quic' | awk '{print " --redefine-sym " $$NF "=" $$NF "_YB80VJ"}') && \
280-
$(TARGET)-objcopy $$renameSymbols ./dep/_msquic/$(TARGET)/bin/Release/libmsquic.a
293+
$(TARGET)-objcopy $$renameSymbols ./dep/_msquic/$(TARGET)/bin/Release/libmsquic.a
294+
295+
compile_p2p:
296+
cd ./dep/p2p && \
297+
. "$$HOME/.cargo/env" && \
298+
cargo build -r --target $(RUST_TARGET)
299+
300+
clean_p2p:
301+
cd ./dep/p2p && \
302+
. "$$HOME/.cargo/env" && \
303+
cargo clean

client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func New(args []string, out io.Writer) (c *Client, err error) {
8989
c = &Client{
9090
Logger: l,
9191
tunnels: make(map[*conn]struct{}),
92-
peers: make(map[uint32]*peerTask),
92+
peers: make(map[uint32]PeerTask),
9393
}
9494
c.config.Store(&conf)
9595
c.tunnelsCond = sync.NewCond(c.tunnelsRWMtx.RLocker())

client/config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ type Options struct {
6666
SentryDebug bool `yaml:"sentryDebug,omitempty" json:",omitempty" usage:"Sentry debug mode, the debug information is printed to help you understand what sentry is doing"`
6767

6868
WebRTCConnectionIdleTimeout config.Duration `yaml:"webrtcConnectionIdleTimeout,omitempty" usage:"The timeout of WebRTC connection. Supports values like '30s', '5m'"`
69-
WebRTCRemoteConnections uint `yaml:"webrtcConnections" usage:"The max number of webrtc connections. Valid value is 1 to 50"`
69+
WebRTCRemoteConnections uint `yaml:"webrtcConnections" usage:"The max number of webrtc connections. Valid value is 1 to 50"`
7070
WebRTCLogLevel string `yaml:"webrtcLogLevel,omitempty" json:",omitempty" usage:"WebRTC log level: verbose, info, warning, error"`
7171
WebRTCMinPort uint16 `yaml:"webrtcMinPort,omitempty" json:",omitempty" usage:"The min port of WebRTC peer connection"`
7272
WebRTCMaxPort uint16 `yaml:"webrtcMaxPort,omitempty" json:",omitempty" usage:"The max port of WebRTC peer connection"`
73+
WebRTCThread bool `yaml:"webrtcThreadMode,omitempty" json:",omitempty" usage:"Use thread mode of WebRTC peer connection"`
7374

7475
TCPForwardAddr string `yaml:"tcpForwardAddr,omitempty" json:",omitempty" usage:"The address of TCP forward"`
7576
TCPForwardHostPrefix string `yaml:"tcpForwardHostPrefix,omitempty" json:",omitempty" usage:"The host prefix of TCP forward"`
@@ -168,6 +169,8 @@ type service struct {
168169
LocalURL clientURL `yaml:"local,omitempty" json:",omitempty"`
169170
LocalTimeout config.Duration `yaml:"localTimeout,omitempty" json:",omitempty"`
170171
UseLocalAsHTTPHost bool `yaml:"useLocalAsHTTPHost,omitempty" json:",omitempty"`
172+
173+
remoteTCPPort uint32
171174
}
172175

173176
func (s *service) String() string {

client/conn.go

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,10 @@ func (c *conn) processData(taskID uint32, r *bufio.LimitedReader) (readErr, writ
461461
})
462462
return
463463
}
464-
_, err := r.WriteTo(pt.apiConn.PipeWriter)
464+
_, err := r.WriteTo(pt.APIWriter())
465465
if err != nil {
466-
pt.Logger.Error().Err(err).Msg("processP2P WriteTo failed")
466+
c.Logger.Info().
467+
Uint32("peerTask", taskID).Msg("got closed because task with same id is received")
467468
}
468469
return
469470
}
@@ -494,17 +495,65 @@ func (c *conn) processData(taskID uint32, r *bufio.LimitedReader) (readErr, writ
494495
}
495496

496497
func (c *conn) processP2P(id uint32, r *bufio.LimitedReader) {
497-
t, ok := c.newPeerTask(id)
498+
var t PeerTask
499+
var ok bool
500+
if c.client.Config().WebRTCThread {
501+
t, ok = c.newPeerTask(id)
502+
} else {
503+
t, ok = c.newPeerProcTask(id)
504+
}
498505
if !ok {
499506
return
500507
}
501508

502-
c.client.apiServer.Listener.AcceptCh() <- t.apiConn
503-
t.Logger.Info().Msg("peer task started")
504-
_, err := r.WriteTo(t.apiConn.PipeWriter)
509+
c.client.apiServer.Listener.AcceptCh() <- t.APIConn()
510+
c.Logger.Info().
511+
Uint32("peerTask", id).Msg("peer task started")
512+
_, err := r.WriteTo(t.APIWriter())
505513
if err != nil {
506-
t.Logger.Error().Err(err).Msg("processP2P WriteTo failed")
514+
c.Logger.Error().
515+
Uint32("peerTask", id).Err(err).Msg("processP2P WriteTo failed")
516+
}
517+
}
518+
519+
func (c *conn) newPeerProcTask(id uint32) (t *peerProcessTask, ok bool) {
520+
c.client.peersRWMtx.Lock()
521+
defer c.client.peersRWMtx.Unlock()
522+
if !predef.Debug {
523+
l := uint(len(c.client.peers))
524+
if l >= c.client.Config().WebRTCRemoteConnections {
525+
respAndClose(id, c, [][]byte{
526+
[]byte("HTTP/1.1 403 Forbidden\r\nConnection: Closed\r\n\r\n"),
527+
})
528+
return
529+
}
530+
}
531+
532+
t = &peerProcessTask{}
533+
t.id = id
534+
t.tunnel = c
535+
t.apiConn = api.NewConn(id, "", c)
536+
t.apiConn.ProcessOffer = t.processOffer
537+
t.apiConn.GetOffer = t.getOffer
538+
t.apiConn.ProcessAnswer = t.processAnswer
539+
t.data = pool.BytesPool.Get().([]byte)
540+
t.Logger = c.Logger.With().
541+
Uint32("peerTask", id).
542+
Logger()
543+
t.timer = time.AfterFunc(120*time.Second, func() {
544+
t.Logger.Info().Msg("peer task timeout")
545+
t.CloseWithLock()
546+
})
547+
548+
ot, ok := c.client.peers[id]
549+
if ok && ot != nil {
550+
ot.CloseWithLock()
551+
c.Logger.Info().
552+
Uint32("peerTask", id).Msg("got closed because task with same id is received")
507553
}
554+
c.client.peers[id] = t
555+
ok = true
556+
return
508557
}
509558

510559
func (c *conn) newPeerTask(id uint32) (t *peerTask, ok bool) {
@@ -542,7 +591,8 @@ func (c *conn) newPeerTask(id uint32) (t *peerTask, ok bool) {
542591
ot, ok := c.client.peers[id]
543592
if ok && ot != nil {
544593
ot.CloseWithLock()
545-
ot.Logger.Info().Msg("got closed because task with same id is received")
594+
c.Logger.Info().
595+
Uint32("peerTask", id).Msg("got closed because task with same id is received")
546596
}
547597
c.client.peers[id] = t
548598
ok = true

client/debug.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type Client struct {
3434
closing uint32
3535
tunnels map[*conn]struct{}
3636
tunnelsRWMtx sync.RWMutex
37-
peers map[uint32]*peerTask
37+
peers map[uint32]PeerTask
3838
peersRWMtx sync.RWMutex
3939
tunnelsCond *sync.Cond
4040
idleManager *idleManager

client/peer.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ type peerTask struct {
5555
waitNegotiationNeeded chan struct{}
5656
}
5757

58+
func (pt *peerTask) APIConn() *api.Conn {
59+
return pt.apiConn
60+
}
61+
62+
func (pt *peerTask) APIWriter() *std.PipeWriter {
63+
return pt.apiConn.PipeWriter
64+
}
65+
5866
func (pt *peerTask) OnSignalingChange(state webrtc.SignalingState) {
5967
pt.Logger.Info().Str("state", state.String()).Msg("signaling state changed")
6068
}
@@ -492,7 +500,7 @@ func (pt *peerTask) processAnswer(r *http.Request, writer http.ResponseWriter) {
492500
err = errors.New("invalid task id")
493501
return
494502
}
495-
task = pt
503+
task = pt.(*peerTask)
496504
}
497505
task.process(r.Body, writer, func() (err error) {
498506
var answer webrtc.SessionDescription

0 commit comments

Comments
 (0)