mirror of
https://github.com/balzack/databag.git
synced 2025-02-11 19:19:16 +00:00
enabling sturn sessions
This commit is contained in:
parent
671e2e7587
commit
ef837e368f
@ -2,8 +2,11 @@ package sturn
|
||||
|
||||
import (
|
||||
// "encoding/json"
|
||||
"encoding/hex"
|
||||
"sync"
|
||||
"github.com/theckman/go-securerandom"
|
||||
// "time"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
@ -12,19 +15,27 @@ var sturn *Sturn
|
||||
const SturnKeepAlive = 3600
|
||||
const SturnMaxSize = 1024
|
||||
const SturnMaxBindFail = 16
|
||||
const SturnNonceSize = 8
|
||||
const SturnPassSize = 8
|
||||
|
||||
type SturnSession struct {
|
||||
user string
|
||||
auth string
|
||||
nonce string
|
||||
}
|
||||
|
||||
type Sturn struct {
|
||||
sync sync.Mutex
|
||||
sessions []*SturnSession
|
||||
sessionId int
|
||||
sessions map[string]*SturnSession
|
||||
closing bool
|
||||
port uint
|
||||
relayStart uint
|
||||
relayEnd uint
|
||||
conn *net.PacketConn
|
||||
closed chan bool
|
||||
buf []byte
|
||||
publicIp string
|
||||
}
|
||||
|
||||
func Listen(port uint, relayStart uint, relayEnd uint) (error) {
|
||||
@ -41,9 +52,9 @@ func Listen(port uint, relayStart uint, relayEnd uint) (error) {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("STARTED STURN SERVER")
|
||||
|
||||
sturn := &Sturn{
|
||||
sessionId: 0,
|
||||
closing: false,
|
||||
port: port,
|
||||
relayStart: relayStart,
|
||||
relayEnd: relayEnd,
|
||||
@ -71,12 +82,58 @@ func (s *Sturn) serve(conn net.PacketConn) {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
s.handleMessage(buf[:n], addr);
|
||||
}
|
||||
|
||||
// TODO terminate all sessions
|
||||
s.sync.Lock()
|
||||
s.closing = true
|
||||
for _, session := range s.sessions {
|
||||
// TODO cleanup session
|
||||
fmt.Println(session)
|
||||
}
|
||||
s.sync.Unlock()
|
||||
|
||||
s.closed <- true
|
||||
}
|
||||
|
||||
func TestSession() {
|
||||
if sturn != nil {
|
||||
sturn.sync.Lock()
|
||||
defer sturn.sync.Unlock()
|
||||
if sturn.closing {
|
||||
return
|
||||
}
|
||||
session := &SturnSession{
|
||||
user: "user",
|
||||
auth: "pass",
|
||||
nonce: "noncynoncenonce",
|
||||
}
|
||||
sturn.sessions["user"] = session
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Sturn) addSession() (*SturnSession, error) {
|
||||
s.sync.Lock()
|
||||
defer s.sync.Unlock()
|
||||
if !s.closing {
|
||||
return nil, errors.New("closing sturn")
|
||||
}
|
||||
s.sessionId += 1
|
||||
user := fmt.Sprintf("%08d", s.sessionId)
|
||||
authBin, authErr := securerandom.Bytes(SturnPassSize)
|
||||
if authErr != nil {
|
||||
return nil, authErr
|
||||
}
|
||||
nonceBin, nonceErr := securerandom.Bytes(SturnNonceSize)
|
||||
if nonceErr != nil {
|
||||
return nil, nonceErr
|
||||
}
|
||||
session := &SturnSession{
|
||||
user: user,
|
||||
auth: hex.EncodeToString(authBin),
|
||||
nonce: hex.EncodeToString(nonceBin),
|
||||
}
|
||||
s.sessions[user] = session
|
||||
return session, nil
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@ func main() {
|
||||
methods := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"})
|
||||
|
||||
sturn.Listen(5001, 5002, 5101)
|
||||
sturn.TestSession()
|
||||
|
||||
args := os.Args
|
||||
if len(args) == 3 {
|
||||
|
Loading…
Reference in New Issue
Block a user