mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
91 lines
1.7 KiB
Go
91 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/keroserene/go-webrtc"
|
|
"log"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
LogTimeInterval = 5
|
|
)
|
|
|
|
type IceServerList []webrtc.ConfigurationOption
|
|
|
|
func (i *IceServerList) String() string {
|
|
return fmt.Sprint(*i)
|
|
}
|
|
|
|
func (i *IceServerList) Set(s string) error {
|
|
log.Println("IceServerList:")
|
|
for _, server := range strings.Split(s, ",") {
|
|
// TODO: STUN / TURN url format validation?
|
|
log.Println(server)
|
|
option := webrtc.OptionIceServer(server)
|
|
*i = append(*i, option)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type BytesInfo struct {
|
|
outboundChan chan int
|
|
inboundChan chan int
|
|
outbound int
|
|
inbound int
|
|
outEvents int
|
|
inEvents int
|
|
isLogging bool
|
|
}
|
|
|
|
func (b *BytesInfo) Log() {
|
|
b.isLogging = true
|
|
var amount int
|
|
output := func() {
|
|
log.Printf("Traffic Bytes (in|out): %d | %d -- (%d OnMessages, %d Sends)",
|
|
b.inbound, b.outbound, b.inEvents, b.outEvents)
|
|
b.outbound = 0
|
|
b.outEvents = 0
|
|
b.inbound = 0
|
|
b.inEvents = 0
|
|
}
|
|
last := time.Now()
|
|
for {
|
|
select {
|
|
case amount = <-b.outboundChan:
|
|
b.outbound += amount
|
|
b.outEvents++
|
|
last := time.Now()
|
|
if time.Since(last) > time.Second*LogTimeInterval {
|
|
last = time.Now()
|
|
output()
|
|
}
|
|
case amount = <-b.inboundChan:
|
|
b.inbound += amount
|
|
b.inEvents++
|
|
if time.Since(last) > time.Second*LogTimeInterval {
|
|
last = time.Now()
|
|
output()
|
|
}
|
|
case <-time.After(time.Second * LogTimeInterval):
|
|
if b.inEvents > 0 || b.outEvents > 0 {
|
|
output()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (b *BytesInfo) AddOutbound(amount int) {
|
|
if !b.isLogging {
|
|
return
|
|
}
|
|
b.outboundChan <- amount
|
|
}
|
|
|
|
func (b *BytesInfo) AddInbound(amount int) {
|
|
if !b.isLogging {
|
|
return
|
|
}
|
|
b.inboundChan <- amount
|
|
}
|