From 3d1341cfe116e9c50e8b8728b1d3f914ae8a8af2 Mon Sep 17 00:00:00 2001 From: Craig Date: Mon, 10 Feb 2020 07:13:40 +0000 Subject: [PATCH] potential fix for #25 --- .docker/build | 2 +- client/src/neko/base.ts | 15 ++++++--- client/vue.config.js | 1 + server/internal/types/config/webrtc.go | 46 +++++++++++++++++++------- server/internal/utils/ip.go | 24 ++++++++++++++ server/internal/webrtc/webrtc.go | 9 ++--- 6 files changed, 74 insertions(+), 23 deletions(-) create mode 100644 server/internal/utils/ip.go diff --git a/.docker/build b/.docker/build index 74dab774..512d3298 100755 --- a/.docker/build +++ b/.docker/build @@ -1,6 +1,6 @@ #!/bin/bash -# sudo docker run -p 80:8080 -p 59000-59100:59000-59100/udp --cap-add SYS_ADMIN nurdism/neko:chromium +# sudo docker run -p 80:8080 -p 59000-59100:59000-59100/udp --cap-add SYS_ADMIN --shm-size=1gb nurdism/neko:chromium # sudo docker run -p 80:8080 -p 59000-59100:59000-59100/udp --shm-size=1gb nurdism/neko:firefox # sudo docker run --network host --shm-size=1gb -it nurdism/neko:base /bin/bash diff --git a/client/src/neko/base.ts b/client/src/neko/base.ts index 7998098d..534694be 100644 --- a/client/src/neko/base.ts +++ b/client/src/neko/base.ts @@ -151,9 +151,7 @@ export abstract class BaseClient extends EventEmitter { return } - this._peer = new RTCPeerConnection({ - iceServers: [{ urls: 'stun:stun.l.google.com:19302' }], - }) + this._peer = new RTCPeerConnection() this._peer.onicecandidate = event => { if (event.candidate === null && this._peer!.localDescription) { @@ -167,9 +165,18 @@ export abstract class BaseClient extends EventEmitter { } } + this._peer.onsignalingstatechange = event => { + this.emit('debug', `peer signal state chagned: ${this._peer!.signalingState}`) + } + + this._peer.onconnectionstatechange = event => { + this.emit('debug', `peer connection state chagned: ${this._peer!.connectionState}`) + } + this._peer.oniceconnectionstatechange = event => { this._state = this._peer!.iceConnectionState - this.emit('debug', `peer connection state chagned: ${this._state}`) + + this.emit('debug', `peer ice connection state chagned: ${this._peer!.iceConnectionState}`) switch (this._state) { case 'checking': diff --git a/client/vue.config.js b/client/vue.config.js index 3a0748ba..2ade23d0 100644 --- a/client/vue.config.js +++ b/client/vue.config.js @@ -1,6 +1,7 @@ const path = require('path') module.exports = { + productionSourceMap: false, css: { loaderOptions: { sass: { diff --git a/server/internal/types/config/webrtc.go b/server/internal/types/config/webrtc.go index 094cdc96..875fd5a7 100644 --- a/server/internal/types/config/webrtc.go +++ b/server/internal/types/config/webrtc.go @@ -7,17 +7,19 @@ import ( "github.com/pion/webrtc/v2" "github.com/spf13/cobra" "github.com/spf13/viper" + "n.eko.moe/neko/internal/utils" ) type WebRTC struct { - Device string - AudioCodec string - AudioParams string - Display string - VideoCodec string - VideoParams string - EphemeralStart uint16 - EphemeralEnd uint16 + Device string + AudioCodec string + AudioParams string + Display string + VideoCodec string + VideoParams string + EphemeralMin uint16 + EphemeralMax uint16 + NAT1To1IPs []string } func (WebRTC) Init(cmd *cobra.Command) error { @@ -83,6 +85,11 @@ func (WebRTC) Init(cmd *cobra.Command) error { return err } + cmd.PersistentFlags().StringSlice("ip", []string{}, "sets a list of external IP addresses of 1:1 (D)NAT and a candidate type for which the external IP address is used") + if err := viper.BindPFlag("ip", cmd.PersistentFlags().Lookup("ip")); err != nil { + return err + } + return nil } @@ -113,19 +120,34 @@ func (s *WebRTC) Set() { s.Display = viper.GetString("display") s.VideoCodec = videoCodec s.VideoParams = viper.GetString("vparams") - s.EphemeralStart = 59000 - s.EphemeralEnd = 59100 + s.NAT1To1IPs = viper.GetStringSlice("ip") + ip, err := utils.GetIP() + if err == nil { + s.NAT1To1IPs = append(s.NAT1To1IPs, ip) + } + + min := uint16(59000) + max := uint16(59100) epr := viper.GetString("epr") ports := strings.SplitN(epr, "-", -1) if len(ports[0]) > 1 { start, err := strconv.ParseUint(ports[0], 16, 16) if err == nil { - s.EphemeralStart = uint16(start) + min = uint16(start) } + end, err := strconv.ParseUint(ports[1], 16, 16) if err == nil { - s.EphemeralEnd = uint16(end) + max = uint16(end) } } + + if min > max { + s.EphemeralMin = max + s.EphemeralMax = min + } else { + s.EphemeralMin = min + s.EphemeralMax = max + } } diff --git a/server/internal/utils/ip.go b/server/internal/utils/ip.go new file mode 100644 index 00000000..69798a86 --- /dev/null +++ b/server/internal/utils/ip.go @@ -0,0 +1,24 @@ +package utils + +import ( + "bytes" + "io/ioutil" + "net/http" +) + +// dig @resolver1.opendns.com ANY myip.opendns.com +short -4 + +func GetIP() (string, error) { + rsp, err := http.Get("http://checkip.amazonaws.com") + if err != nil { + return "", err + } + defer rsp.Body.Close() + + buf, err := ioutil.ReadAll(rsp.Body) + if err != nil { + return "", err + } + + return string(bytes.TrimSpace(buf)), nil +} diff --git a/server/internal/webrtc/webrtc.go b/server/internal/webrtc/webrtc.go index 90f79f5d..a25f308b 100644 --- a/server/internal/webrtc/webrtc.go +++ b/server/internal/webrtc/webrtc.go @@ -22,8 +22,10 @@ func New(sessions types.SessionManager, config *config.WebRTC) *WebRTCManager { }, } + settings.SetLite(true) settings.SetNetworkTypes([]webrtc.NetworkType{webrtc.NetworkTypeUDP4}) - settings.SetEphemeralUDPPortRange(config.EphemeralStart, config.EphemeralEnd) + settings.SetEphemeralUDPPortRange(config.EphemeralMin, config.EphemeralMax) + settings.SetNAT1To1IPs(config.NAT1To1IPs, webrtc.ICECandidateTypeHost) return &WebRTCManager{ logger: logger, @@ -33,11 +35,6 @@ func New(sessions types.SessionManager, config *config.WebRTC) *WebRTCManager { sessions: sessions, config: config, configuration: &webrtc.Configuration{ - ICEServers: []webrtc.ICEServer{ - { - URLs: []string{"stun:stun.l.google.com:19302"}, - }, - }, SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback, }, }