mirror of
https://github.com/yibaiba/hideck.git
synced 2026-12-05 16:02:17 +00:00
fix(vowifi): run the real SWu tunnel lifecycle
This commit is contained in:
1 parent
9675d7aa8f
commit
7072fa690a
9 files changed
+477
-43
No files matched your search
+35
-5
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -21,6 +22,9 @@ type Config struct {
|
||||
EPDGAddr string
|
||||
// LocalIP is the local address to bind the IKE/ESP socket to.
|
||||
LocalIP net.IP
|
||||
// ProxyAddr and Proxy route IKE/ESP through a SOCKS5 UDP associate.
|
||||
ProxyAddr string
|
||||
Proxy *ipsec.Socks5Config
|
||||
// IMSI is the subscriber IMSI used for the EAP-AKA identity.
|
||||
IMSI string
|
||||
// MCC/MNC override the MCC/MNC derived from the IMSI in the NAI.
|
||||
@@ -124,7 +128,7 @@ type Session struct {
|
||||
cfg *Config
|
||||
|
||||
// --- transport ---
|
||||
socket *ipsec.SocketManager
|
||||
socket ipsec.Transport
|
||||
|
||||
// --- IKE_AUTH state ---
|
||||
stage ikeAuthStage
|
||||
@@ -391,15 +395,19 @@ func (s *Session) buildTransport() error {
|
||||
if h, p, err := net.SplitHostPort(s.cfg.EPDGAddr); err == nil {
|
||||
host, port = h, p
|
||||
}
|
||||
if strings.TrimSpace(s.cfg.ProxyAddr) != "" {
|
||||
return s.buildProxyTransport(host, port)
|
||||
}
|
||||
localIP := s.cfg.LocalIP
|
||||
if localIP == nil {
|
||||
ip, err := detectOutboundIPv4()
|
||||
ip, err := detectOutboundIPv4(host, port)
|
||||
if err != nil {
|
||||
return fmt.Errorf("detect outbound IP: %w", err)
|
||||
}
|
||||
localIP = ip
|
||||
}
|
||||
sm, err := ipsec.NewSocketManager(localIP.String(), localIP.String(), host, port)
|
||||
localAddr := net.JoinHostPort(localIP.String(), "0")
|
||||
sm, err := ipsec.NewSocketManager(localIP.String(), localAddr, host, port)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open IKE socket: %w", err)
|
||||
}
|
||||
@@ -410,9 +418,31 @@ func (s *Session) buildTransport() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Session) buildProxyTransport(host, port string) error {
|
||||
proxyCfg := ipsec.Socks5Config{}
|
||||
if s.cfg.Proxy != nil {
|
||||
proxyCfg = *s.cfg.Proxy
|
||||
}
|
||||
targetAddr := net.JoinHostPort(host, port)
|
||||
transport, err := ipsec.NewSocks5Transport(proxyCfg, s.cfg.ProxyAddr, targetAddr, 10*time.Second)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open SOCKS5 IKE transport: %w", err)
|
||||
}
|
||||
s.socket = transport
|
||||
s.remoteIP = transport.RemoteIP()
|
||||
s.remotePort = transport.RemotePort()
|
||||
transport.Start()
|
||||
return nil
|
||||
}
|
||||
|
||||
// detectOutboundIPv4 finds the local IPv4 address used to reach the ePDG.
|
||||
func detectOutboundIPv4() (net.IP, error) {
|
||||
conn, err := net.Dial("udp4", "8.8.8.8:53")
|
||||
func detectOutboundIPv4(host, port string) (net.IP, error) {
|
||||
remoteIP, err := detectOutboundRoute(host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
remote := net.JoinHostPort(remoteIP.String(), port)
|
||||
conn, err := net.Dial("udp", remote)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package swu
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBuildTransportBindsLocalIPWithEphemeralPort(t *testing.T) {
|
||||
remote, err := net.ListenUDP("udp4", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 0})
|
||||
if err != nil {
|
||||
t.Fatalf("ListenUDP: %v", err)
|
||||
}
|
||||
defer remote.Close()
|
||||
|
||||
session := NewSession(&Config{
|
||||
EPDGAddr: remote.LocalAddr().String(),
|
||||
LocalIP: net.ParseIP("127.0.0.1"),
|
||||
})
|
||||
if err := session.buildTransport(); err != nil {
|
||||
t.Fatalf("buildTransport: %v", err)
|
||||
}
|
||||
defer session.stopTransport()
|
||||
if session.socket.LocalPort() == 0 {
|
||||
t.Fatal("transport did not bind an ephemeral local port")
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ type AKAAppPreference string
|
||||
const (
|
||||
AKAAppPreferenceISIMStrict AKAAppPreference = "isim_strict"
|
||||
AKAAppPreferenceISIM AKAAppPreference = "isim"
|
||||
AKAAppPreferenceUSIMStrict AKAAppPreference = "usim_strict"
|
||||
AKAAppPreferenceUSIM AKAAppPreference = "usim"
|
||||
)
|
||||
|
||||
@@ -29,6 +30,8 @@ func NormalizeAKAApp(s string) AKAAppPreference {
|
||||
return AKAAppPreferenceISIM
|
||||
case "usim":
|
||||
return AKAAppPreferenceUSIM
|
||||
case "usim_strict":
|
||||
return AKAAppPreferenceUSIMStrict
|
||||
default:
|
||||
return AKAAppPreferenceISIM
|
||||
}
|
||||
|
||||
@@ -12,6 +12,9 @@ func TestNormalizeAKAApp(t *testing.T) {
|
||||
if got := NormalizeAKAApp("USIM"); got != AKAAppPreferenceUSIM {
|
||||
t.Errorf("USIM = %q", got)
|
||||
}
|
||||
if got := NormalizeAKAApp("usim_strict"); got != AKAAppPreferenceUSIMStrict {
|
||||
t.Errorf("usim_strict = %q", got)
|
||||
}
|
||||
if got := NormalizeAKAApp(""); got != AKAAppPreferenceISIM {
|
||||
t.Errorf("default = %q", got)
|
||||
}
|
||||
|
||||
+13
-5
@@ -7,6 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/iniwex5/vowifi-go/internal/vowifi/imscore"
|
||||
"github.com/iniwex5/vowifi-go/runtimehost/identity"
|
||||
"github.com/iniwex5/vowifi-go/runtimehost/messaging"
|
||||
)
|
||||
|
||||
@@ -25,10 +26,10 @@ func (stubAKA) CalculateAKA(rand16, autn16 []byte) (imscore.AKAResult, error) {
|
||||
func newTestService(t *testing.T) *imscore.Service {
|
||||
t.Helper()
|
||||
cfg := &imscore.IMSConfig{
|
||||
DeviceID: "dev-1",
|
||||
IMSI: "310260123456789",
|
||||
IMPI: "310260123456789@ims.example.com",
|
||||
Domain: "ims.example.com",
|
||||
DeviceID: "dev-1",
|
||||
IMSI: "310260123456789",
|
||||
IMPI: "310260123456789@ims.example.com",
|
||||
Domain: "ims.example.com",
|
||||
AKAProvider: stubAKA{},
|
||||
}
|
||||
svc, err := imscore.New(cfg)
|
||||
@@ -153,7 +154,14 @@ func TestDeliveryStatusConversion(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestStartInstanceAsync(t *testing.T) {
|
||||
req := StartRequest{Mode: StartModeReader, DeviceID: "dev-1"}
|
||||
prepared := &identity.PreparedSession{
|
||||
Profile: identity.Profile{IMSI: "310260123456789", MCC: "310", MNC: "260"},
|
||||
IMSIdentity: identity.IMSIdentity{IMPI: "310260123456789@ims.example", IMPU: "sip:310260123456789@ims.example", Domain: "ims.example"},
|
||||
EPDGAddr: "epdg.example.com",
|
||||
}
|
||||
req := runtimeTestRequest(prepared, newLifecycleTunnel(nil))
|
||||
req.Mode = StartModeReader
|
||||
req.DeviceID = "dev-1"
|
||||
instCh, errCh := startInstanceAsync(context.Background(), req)
|
||||
select {
|
||||
case inst := <-instCh:
|
||||
|
||||
+75
-2
@@ -46,6 +46,12 @@ func (i *Instance) SetSMSNotifier(n SMSNotifier) {
|
||||
|
||||
// Stop shuts the runtime host down.
|
||||
func (i *Instance) Stop(ctx context.Context) error {
|
||||
if i == nil {
|
||||
return nil
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
i.mu.Lock()
|
||||
if i.stopped {
|
||||
i.mu.Unlock()
|
||||
@@ -53,11 +59,32 @@ func (i *Instance) Stop(ctx context.Context) error {
|
||||
}
|
||||
i.stopped = true
|
||||
svc := i.service
|
||||
tunnel := i.tunnel
|
||||
cancel := i.cancel
|
||||
i.tunnel = nil
|
||||
i.cancel = nil
|
||||
i.mu.Unlock()
|
||||
if cancel != nil {
|
||||
cancel()
|
||||
}
|
||||
if tunnel != nil {
|
||||
tunnel.Shutdown()
|
||||
}
|
||||
if svc != nil {
|
||||
svc.Stop()
|
||||
}
|
||||
return nil
|
||||
i.updateState(func(state *State) {
|
||||
state.SessionState = "stopped"
|
||||
state.DataPlaneUp = false
|
||||
state.TunnelReady = false
|
||||
state.IMSReady = false
|
||||
state.SMSReady = false
|
||||
state.LastReason = "stopped"
|
||||
})
|
||||
if tunnel == nil {
|
||||
return nil
|
||||
}
|
||||
return tunnel.WaitDoneContext(ctx)
|
||||
}
|
||||
|
||||
// StopShared stops the host without tearing down shared resources.
|
||||
@@ -95,10 +122,56 @@ func (i *Instance) Obs() map[string]interface{} {
|
||||
|
||||
// setState updates the runtime state and publishes the change.
|
||||
func (i *Instance) setState(s State) {
|
||||
s.UpdatedAt = time.Now()
|
||||
i.mu.Lock()
|
||||
i.state = s
|
||||
i.mu.Unlock()
|
||||
i.publish(Event{Type: "state", Detail: s.SessionState, Session: i})
|
||||
i.publish(Event{Type: "state", Detail: s.SessionState, State: s, Session: i})
|
||||
}
|
||||
|
||||
func (i *Instance) updateState(update func(*State)) {
|
||||
i.mu.Lock()
|
||||
update(&i.state)
|
||||
i.state.UpdatedAt = time.Now()
|
||||
state := i.state
|
||||
i.mu.Unlock()
|
||||
i.publish(Event{Type: "state", Detail: state.SessionState, State: state, Session: i})
|
||||
}
|
||||
|
||||
func (i *Instance) attachTunnel(tunnel Tunnel, cancel context.CancelFunc) {
|
||||
i.mu.Lock()
|
||||
i.tunnel = tunnel
|
||||
i.cancel = cancel
|
||||
i.mu.Unlock()
|
||||
}
|
||||
|
||||
func (i *Instance) updateTunnelState(sessionState string) {
|
||||
i.updateState(func(state *State) {
|
||||
state.SessionState = sessionState
|
||||
state.TunnelReady = sessionState == "established"
|
||||
state.DataPlaneUp = state.TunnelReady
|
||||
})
|
||||
}
|
||||
|
||||
func (i *Instance) markTunnelEstablished() {
|
||||
i.updateState(func(state *State) {
|
||||
state.SessionState = "established"
|
||||
state.TunnelReady = true
|
||||
state.DataPlaneUp = true
|
||||
state.LastReason = "SWu tunnel established"
|
||||
})
|
||||
}
|
||||
|
||||
func (i *Instance) setStartFailure(err error) {
|
||||
i.updateState(func(state *State) {
|
||||
state.SessionState = "error"
|
||||
state.Error = err.Error()
|
||||
state.LastError = err.Error()
|
||||
state.LastErrorClass = "network"
|
||||
state.LastReason = "SWu tunnel establishment failed"
|
||||
state.TunnelReady = false
|
||||
state.DataPlaneUp = false
|
||||
})
|
||||
}
|
||||
|
||||
// setService installs the IMS service.
|
||||
|
||||
+112
-11
@@ -3,10 +3,15 @@ package runtimehost
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/iniwex5/vowifi-go/engine/ipsec"
|
||||
"github.com/iniwex5/vowifi-go/engine/swu"
|
||||
"github.com/iniwex5/vowifi-go/internal/vowifi/imscore"
|
||||
"github.com/iniwex5/vowifi-go/internal/vowifi/profile"
|
||||
"github.com/iniwex5/vowifi-go/internal/vowifi/runtimecore"
|
||||
"github.com/iniwex5/vowifi-go/runtimehost/eventhost"
|
||||
"github.com/iniwex5/vowifi-go/runtimehost/identity"
|
||||
"github.com/iniwex5/vowifi-go/runtimehost/messaging"
|
||||
@@ -48,6 +53,7 @@ type StartRequest struct {
|
||||
Proxy *ProxyConfig
|
||||
DeliveryStore messaging.DeliveryStore
|
||||
Dispatch eventhost.Dispatcher
|
||||
TunnelFactory TunnelFactory
|
||||
BeforeStart func(context.Context, SessionConfig) error
|
||||
ShouldRun func() bool
|
||||
}
|
||||
@@ -68,26 +74,119 @@ func NewModemAccessAdapter(m Modem) ModemAccessAdapter {
|
||||
|
||||
// Start launches a runtime host for the given request and returns the Instance.
|
||||
func Start(ctx context.Context, req StartRequest) (*Instance, error) {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
if req.ShouldRun != nil && !req.ShouldRun() {
|
||||
return nil, errors.New("runtimehost: start cancelled by ShouldRun")
|
||||
}
|
||||
if err := validateStartRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if req.BeforeStart != nil {
|
||||
if err := req.BeforeStart(ctx, SessionConfig{}); err != nil {
|
||||
if err := req.BeforeStart(ctx, SessionConfig{DataplaneMode: req.Dataplane.Mode}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
inst := &Instance{}
|
||||
inst.setState(State{SessionState: "starting"})
|
||||
|
||||
// Wire the IMS service: build a real imscore service from the prepared
|
||||
// identity when available, otherwise fall back to the placeholder.
|
||||
inst.setState(initialState(req))
|
||||
runCtx, cancel := context.WithCancel(ctx)
|
||||
tunnel, err := newTunnel(req, inst)
|
||||
if err != nil {
|
||||
cancel()
|
||||
inst.setStartFailure(err)
|
||||
return inst, err
|
||||
}
|
||||
inst.attachTunnel(tunnel, cancel)
|
||||
if err := tunnel.Connect(runCtx); err != nil {
|
||||
startErr := fmt.Errorf("runtimehost: SWu tunnel establishment failed: %w", err)
|
||||
_ = inst.Stop(context.Background())
|
||||
inst.setStartFailure(startErr)
|
||||
return inst, startErr
|
||||
}
|
||||
inst.markTunnelEstablished()
|
||||
inst.setService(buildService(req))
|
||||
|
||||
inst.setState(State{SessionState: "established", EPDGAddress: epdgOf(req)})
|
||||
go stopRuntimeOnContext(runCtx, inst)
|
||||
return inst, nil
|
||||
}
|
||||
|
||||
func validateStartRequest(req StartRequest) error {
|
||||
if strings.TrimSpace(req.DeviceID) == "" {
|
||||
return errors.New("runtimehost: device_id is empty")
|
||||
}
|
||||
if req.Prepared == nil {
|
||||
return errors.New("runtimehost: prepared session is required")
|
||||
}
|
||||
if strings.TrimSpace(req.Prepared.EPDGAddr) == "" {
|
||||
return errors.New("runtimehost: prepared session has no ePDG address")
|
||||
}
|
||||
if req.SIM == nil || req.SIM.AKAProvider() == nil {
|
||||
return errors.New("runtimehost: SIM AKA provider is required")
|
||||
}
|
||||
mode := strings.TrimSpace(req.Dataplane.Mode)
|
||||
if mode != "" && mode != swu.DataplaneModeUserspace {
|
||||
return fmt.Errorf("runtimehost: unsupported dataplane mode %q", mode)
|
||||
}
|
||||
if req.Proxy != nil && req.Proxy.Enabled && strings.TrimSpace(req.Proxy.Addr) == "" {
|
||||
return errors.New("runtimehost: enabled SOCKS5 proxy has no address")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func initialState(req StartRequest) State {
|
||||
return State{
|
||||
SessionState: "starting",
|
||||
DeviceID: req.DeviceID, EPDGAddress: epdgOf(req), NetworkMode: req.NetworkMode,
|
||||
DataplaneMode: req.Dataplane.Mode, SIMReady: true, AccessReady: req.Access != nil,
|
||||
}
|
||||
}
|
||||
|
||||
func newTunnel(req StartRequest, inst *Instance) (Tunnel, error) {
|
||||
prepared := preparedForRuntimeCore(req.Prepared)
|
||||
cfg, err := runtimecore.BuildSWUConfig(prepared, req.SIM.AKAProvider())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg.OnStateChange = inst.updateTunnelState
|
||||
if req.Proxy != nil && req.Proxy.Enabled {
|
||||
cfg.ProxyAddr = strings.TrimSpace(req.Proxy.Addr)
|
||||
cfg.Proxy = &ipsec.Socks5Config{Username: req.Proxy.Username, Password: req.Proxy.Password}
|
||||
}
|
||||
factory := req.TunnelFactory
|
||||
if factory == nil {
|
||||
factory = func(cfg *swu.Config) (Tunnel, error) { return swu.NewSession(cfg), nil }
|
||||
}
|
||||
tunnel, err := factory(cfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("runtimehost: create SWu tunnel: %w", err)
|
||||
}
|
||||
if tunnel == nil {
|
||||
return nil, errors.New("runtimehost: create SWu tunnel: nil session")
|
||||
}
|
||||
return tunnel, nil
|
||||
}
|
||||
|
||||
func preparedForRuntimeCore(prepared *identity.PreparedSession) *runtimecore.PreparedSessionStart {
|
||||
return &runtimecore.PreparedSessionStart{
|
||||
Profile: profile.Profile{
|
||||
IMSI: prepared.Profile.IMSI, MCC: prepared.Profile.MCC, MNC: prepared.Profile.MNC,
|
||||
IMEI: prepared.Profile.IMEI, SMSC: prepared.Profile.SMSC,
|
||||
},
|
||||
IMSIdentity: profile.IMSIdentity{
|
||||
IMPI: prepared.IMSIdentity.IMPI, IMPU: []string{prepared.IMSIdentity.IMPU},
|
||||
Domain: prepared.IMSIdentity.Domain,
|
||||
},
|
||||
AuthPlan: profile.AuthPlan{AKAApp: profile.NormalizeAKAApp(string(prepared.IMSIdentity.AKAAppPreference))},
|
||||
EPDGAddr: prepared.EPDGAddr, EPDGSource: prepared.EPDGSource,
|
||||
}
|
||||
}
|
||||
|
||||
func stopRuntimeOnContext(ctx context.Context, inst *Instance) {
|
||||
<-ctx.Done()
|
||||
_ = inst.Stop(context.Background())
|
||||
}
|
||||
|
||||
// buildService builds the IMS service for the request: a real imscore
|
||||
// service when the prepared identity is present, otherwise the placeholder.
|
||||
func buildService(req StartRequest) Service {
|
||||
@@ -173,10 +272,12 @@ func (s *hostService) SendUSSD(ctx context.Context, code string) (*messaging.USS
|
||||
func (s *hostService) ContinueUSSD(ctx context.Context, sessionID, input string) (*messaging.USSDResult, error) {
|
||||
return nil, errors.New("runtimehost: USSD service not available")
|
||||
}
|
||||
func (s *hostService) CancelUSSD(ctx context.Context, sessionID string) error { return errors.New("runtimehost: USSD service not available") }
|
||||
func (s *hostService) Status() Status { return Status{} }
|
||||
func (s *hostService) StatusSnapshot() Status { return Status{} }
|
||||
func (s *hostService) Stop() {}
|
||||
func (s *hostService) CancelUSSD(ctx context.Context, sessionID string) error {
|
||||
return errors.New("runtimehost: USSD service not available")
|
||||
}
|
||||
func (s *hostService) Status() Status { return Status{} }
|
||||
func (s *hostService) StatusSnapshot() Status { return Status{} }
|
||||
func (s *hostService) Stop() {}
|
||||
func (s *hostService) TriggerRegisterImmediate() {}
|
||||
|
||||
// WithTraceID returns a context carrying the given trace id.
|
||||
|
||||
+195
-20
@@ -2,9 +2,14 @@ package runtimehost
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
enginesim "github.com/iniwex5/vowifi-go/engine/sim"
|
||||
"github.com/iniwex5/vowifi-go/engine/swu"
|
||||
"github.com/iniwex5/vowifi-go/runtimehost/identity"
|
||||
)
|
||||
|
||||
@@ -27,6 +32,85 @@ func (stubProvider) GetISIMIdentity() (identity.Identity, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
type startAKAProvider struct{}
|
||||
|
||||
func (startAKAProvider) CalculateAKA(_, _ []byte) (enginesim.AKAResult, error) {
|
||||
return enginesim.AKAResult{RES: []byte{1}, CK: make([]byte, 16), IK: make([]byte, 16)}, nil
|
||||
}
|
||||
|
||||
type startSIMAdapter struct{}
|
||||
|
||||
func (startSIMAdapter) AKAProvider() enginesim.AKAProvider { return startAKAProvider{} }
|
||||
|
||||
type lifecycleTunnel struct {
|
||||
mu sync.Mutex
|
||||
state string
|
||||
connectErr error
|
||||
done chan struct{}
|
||||
connectCalled chan struct{}
|
||||
shutdownOnce sync.Once
|
||||
onStateChange func(string)
|
||||
}
|
||||
|
||||
func newLifecycleTunnel(connectErr error) *lifecycleTunnel {
|
||||
return &lifecycleTunnel{
|
||||
state: "idle", connectErr: connectErr, done: make(chan struct{}), connectCalled: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (t *lifecycleTunnel) Connect(context.Context) error {
|
||||
t.setState("connecting")
|
||||
close(t.connectCalled)
|
||||
if t.connectErr != nil {
|
||||
t.setState("error")
|
||||
return t.connectErr
|
||||
}
|
||||
t.setState("established")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *lifecycleTunnel) Shutdown() {
|
||||
t.shutdownOnce.Do(func() {
|
||||
t.setState("shutdown")
|
||||
close(t.done)
|
||||
})
|
||||
}
|
||||
|
||||
func (t *lifecycleTunnel) State() string {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
return t.state
|
||||
}
|
||||
|
||||
func (t *lifecycleTunnel) WaitDoneContext(ctx context.Context) error {
|
||||
select {
|
||||
case <-t.done:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
func (t *lifecycleTunnel) setState(state string) {
|
||||
t.mu.Lock()
|
||||
t.state = state
|
||||
t.mu.Unlock()
|
||||
if t.onStateChange != nil {
|
||||
t.onStateChange(state)
|
||||
}
|
||||
}
|
||||
|
||||
func runtimeTestRequest(prepared *identity.PreparedSession, tunnel *lifecycleTunnel) StartRequest {
|
||||
return StartRequest{
|
||||
Mode: StartModeMain, DeviceID: "wwan0", TraceID: "trace-1", Prepared: prepared,
|
||||
SIM: startSIMAdapter{}, Dataplane: DataplanePolicy{Mode: swu.DataplaneModeUserspace},
|
||||
TunnelFactory: func(cfg *swu.Config) (Tunnel, error) {
|
||||
tunnel.onStateChange = cfg.OnStateChange
|
||||
return tunnel, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestStart(t *testing.T) {
|
||||
prepared, err := identity.PrepareStart(identity.PrepareStartInput{
|
||||
DeviceID: "wwan0",
|
||||
@@ -37,15 +121,15 @@ func TestStart(t *testing.T) {
|
||||
t.Fatalf("PrepareStart: %v", err)
|
||||
}
|
||||
var beforeCalled bool
|
||||
inst, err := Start(context.Background(), StartRequest{
|
||||
Mode: StartModeMain,
|
||||
DeviceID: "wwan0",
|
||||
TraceID: "trace-1",
|
||||
Profile: prepared.Profile,
|
||||
Prepared: &prepared,
|
||||
BeforeStart: func(_ context.Context, _ SessionConfig) error { beforeCalled = true; return nil },
|
||||
ShouldRun: func() bool { return true },
|
||||
})
|
||||
tunnel := newLifecycleTunnel(nil)
|
||||
req := runtimeTestRequest(&prepared, tunnel)
|
||||
req.Profile = prepared.Profile
|
||||
req.BeforeStart = func(_ context.Context, cfg SessionConfig) error {
|
||||
beforeCalled = cfg.DataplaneMode == swu.DataplaneModeUserspace
|
||||
return nil
|
||||
}
|
||||
req.ShouldRun = func() bool { return true }
|
||||
inst, err := Start(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatalf("Start: %v", err)
|
||||
}
|
||||
@@ -58,6 +142,17 @@ func TestStart(t *testing.T) {
|
||||
if inst.State().EPDGAddress == "" {
|
||||
t.Error("EPDG address not set from prepared session")
|
||||
}
|
||||
select {
|
||||
case <-tunnel.connectCalled:
|
||||
default:
|
||||
t.Error("SWu Connect was not called")
|
||||
}
|
||||
if !inst.State().TunnelReady || !inst.State().DataPlaneUp {
|
||||
t.Errorf("tunnel state = %+v", inst.State())
|
||||
}
|
||||
if err := inst.Stop(context.Background()); err != nil {
|
||||
t.Fatalf("Stop: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartShouldRunFalse(t *testing.T) {
|
||||
@@ -74,19 +169,18 @@ func TestNewModemAccessAdapter(t *testing.T) {
|
||||
}
|
||||
}
|
||||
func TestStartWiresRealIMSService(t *testing.T) {
|
||||
req := StartRequest{
|
||||
Mode: StartModeMain,
|
||||
DeviceID: "dev-1",
|
||||
TraceID: "trace-1",
|
||||
Prepared: &identity.PreparedSession{
|
||||
IMSIdentity: identity.IMSIdentity{
|
||||
IMPI: "310260123456789@ims.mnc026.mcc310.3gppnetwork.org",
|
||||
IMPU: "sip:310260123456789@ims.mnc026.mcc310.3gppnetwork.org",
|
||||
Domain: "ims.mnc026.mcc310.3gppnetwork.org",
|
||||
},
|
||||
EPDGAddr: "epdg.example.com",
|
||||
prepared := &identity.PreparedSession{
|
||||
Profile: identity.Profile{IMSI: "310260123456789", MCC: "310", MNC: "260"},
|
||||
IMSIdentity: identity.IMSIdentity{
|
||||
IMPI: "310260123456789@ims.mnc026.mcc310.3gppnetwork.org",
|
||||
IMPU: "sip:310260123456789@ims.mnc026.mcc310.3gppnetwork.org",
|
||||
Domain: "ims.mnc026.mcc310.3gppnetwork.org",
|
||||
},
|
||||
EPDGAddr: "epdg.example.com",
|
||||
}
|
||||
req := runtimeTestRequest(prepared, newLifecycleTunnel(nil))
|
||||
req.DeviceID = "dev-1"
|
||||
req.Prepared = prepared
|
||||
inst, err := Start(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatalf("Start: %v", err)
|
||||
@@ -107,3 +201,84 @@ func TestStartWiresRealIMSService(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartReturnsFailedInstanceWhenTunnelConnectFails(t *testing.T) {
|
||||
connectErr := errors.New("IKE_AUTH rejected")
|
||||
tunnel := newLifecycleTunnel(connectErr)
|
||||
prepared := &identity.PreparedSession{
|
||||
Profile: identity.Profile{IMSI: "310260123456789", MCC: "310", MNC: "260"},
|
||||
IMSIdentity: identity.IMSIdentity{IMPI: "310260123456789@ims.example", IMPU: "sip:310260123456789@ims.example", Domain: "ims.example"},
|
||||
EPDGAddr: "epdg.example.com",
|
||||
}
|
||||
inst, err := Start(context.Background(), runtimeTestRequest(prepared, tunnel))
|
||||
if !errors.Is(err, connectErr) {
|
||||
t.Fatalf("Start error = %v, want %v", err, connectErr)
|
||||
}
|
||||
if inst == nil || inst.State().SessionState != "error" || !strings.Contains(inst.State().Error, connectErr.Error()) {
|
||||
t.Fatalf("failed instance state = %+v", inst.State())
|
||||
}
|
||||
select {
|
||||
case <-tunnel.done:
|
||||
default:
|
||||
t.Fatal("failed tunnel was not shut down")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartContextCancellationStopsTunnel(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
tunnel := newLifecycleTunnel(nil)
|
||||
prepared := &identity.PreparedSession{
|
||||
Profile: identity.Profile{IMSI: "310260123456789", MCC: "310", MNC: "260"},
|
||||
IMSIdentity: identity.IMSIdentity{IMPI: "310260123456789@ims.example", IMPU: "sip:310260123456789@ims.example", Domain: "ims.example"},
|
||||
EPDGAddr: "epdg.example.com",
|
||||
}
|
||||
inst, err := Start(ctx, runtimeTestRequest(prepared, tunnel))
|
||||
if err != nil {
|
||||
t.Fatalf("Start: %v", err)
|
||||
}
|
||||
stopped := make(chan struct{}, 1)
|
||||
inst.AddObserver(func(_ context.Context, event Event) {
|
||||
if event.State.SessionState == "stopped" {
|
||||
stopped <- struct{}{}
|
||||
}
|
||||
})
|
||||
cancel()
|
||||
select {
|
||||
case <-stopped:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("context cancellation did not stop runtime")
|
||||
}
|
||||
select {
|
||||
case <-tunnel.done:
|
||||
default:
|
||||
t.Fatal("context cancellation did not shut down tunnel")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartPassesSOCKS5ProxyToTunnel(t *testing.T) {
|
||||
tunnel := newLifecycleTunnel(nil)
|
||||
prepared := &identity.PreparedSession{
|
||||
Profile: identity.Profile{IMSI: "310260123456789", MCC: "310", MNC: "260"},
|
||||
IMSIdentity: identity.IMSIdentity{IMPI: "310260123456789@ims.example", IMPU: "sip:310260123456789@ims.example", Domain: "ims.example"},
|
||||
EPDGAddr: "epdg.example.com",
|
||||
}
|
||||
req := runtimeTestRequest(prepared, tunnel)
|
||||
req.Proxy = &ProxyConfig{Enabled: true, Addr: "127.0.0.1:1080", Username: "alice", Password: "secret"}
|
||||
var captured *swu.Config
|
||||
req.TunnelFactory = func(cfg *swu.Config) (Tunnel, error) {
|
||||
captured = cfg
|
||||
tunnel.onStateChange = cfg.OnStateChange
|
||||
return tunnel, nil
|
||||
}
|
||||
inst, err := Start(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatalf("Start: %v", err)
|
||||
}
|
||||
defer inst.Stop(context.Background())
|
||||
if captured == nil || captured.ProxyAddr != "127.0.0.1:1080" || captured.Proxy == nil {
|
||||
t.Fatalf("captured proxy config = %+v", captured)
|
||||
}
|
||||
if captured.Proxy.Username != "alice" || captured.Proxy.Password != "secret" {
|
||||
t.Fatalf("captured proxy credentials mismatch")
|
||||
}
|
||||
}
|
||||
+15
@@ -12,6 +12,7 @@ import (
|
||||
"time"
|
||||
|
||||
enginesim "github.com/iniwex5/vowifi-go/engine/sim"
|
||||
"github.com/iniwex5/vowifi-go/engine/swu"
|
||||
"github.com/iniwex5/vowifi-go/runtimehost/identity"
|
||||
"github.com/iniwex5/vowifi-go/runtimehost/messaging"
|
||||
)
|
||||
@@ -140,6 +141,18 @@ type SIMAdapter interface {
|
||||
AKAProvider() enginesim.AKAProvider
|
||||
}
|
||||
|
||||
// Tunnel is the SWu session lifecycle owned by a runtime Instance.
|
||||
type Tunnel interface {
|
||||
Connect(context.Context) error
|
||||
Shutdown()
|
||||
State() string
|
||||
WaitDoneContext(context.Context) error
|
||||
}
|
||||
|
||||
// TunnelFactory builds an SWu tunnel from the prepared configuration.
|
||||
// Tests and alternate hosts can inject the transport boundary explicitly.
|
||||
type TunnelFactory func(*swu.Config) (Tunnel, error)
|
||||
|
||||
// ProxyConfig configures the ePDG proxy path.
|
||||
type ProxyConfig struct {
|
||||
ID string
|
||||
@@ -174,6 +187,8 @@ type Instance struct {
|
||||
mu sync.RWMutex
|
||||
state State
|
||||
service Service
|
||||
tunnel Tunnel
|
||||
cancel context.CancelFunc
|
||||
observers []ObserverFunc
|
||||
notifier Notifier
|
||||
smsNotifier SMSNotifier
|
||||
|
||||
Reference in new issue
Block a user