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