Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: track netmask passed by the remote #55

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions internal/model/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,19 @@ func (o *Options) ServerOptionsString() string {
// - during the handshake (mtu).
// - after server pushes config options(ip, gw).
type TunnelInfo struct {
MTU int
IP string
GW string
// GW is the Route Gateway.
GW string

// IP is the assigned IP.
IP string

// MTU is the configured MTU pushed by the remote.
MTU int

// NetMask is the netmask configured on the TUN interface, pushed by the ifconfig command.
NetMask string

// PeerID is the peer-id assigned to us by the remote.
PeerID int
}

Expand All @@ -197,9 +207,12 @@ func NewTunnelInfoFromPushedOptions(opts map[string][]string) *TunnelInfo {
} else if r := opts["route-gateway"]; len(r) >= 1 {
t.GW = r[0]
}
ip := opts["ifconfig"]
if len(ip) >= 1 {
t.IP = ip[0]
ifconfig := opts["ifconfig"]
if len(ifconfig) >= 1 {
t.IP = ifconfig[0]
}
if len(ifconfig) >= 2 {
t.NetMask = ifconfig[1]
}
peerID := opts["peer-id"]
if len(peerID) == 1 {
Expand Down
10 changes: 6 additions & 4 deletions internal/session/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ func (m *Manager) UpdateTunnelInfo(ti *model.TunnelInfo) {
m.tunnelInfo.IP = ti.IP
m.tunnelInfo.GW = ti.GW
m.tunnelInfo.PeerID = ti.PeerID
m.tunnelInfo.NetMask = ti.NetMask

m.logger.Infof("Tunnel IP: %s", ti.IP)
m.logger.Infof("Gateway IP: %s", ti.GW)
Expand All @@ -347,9 +348,10 @@ func (m *Manager) TunnelInfo() model.TunnelInfo {
defer m.mu.Unlock()
m.mu.Lock()
return model.TunnelInfo{
MTU: m.tunnelInfo.MTU,
IP: m.tunnelInfo.IP,
GW: m.tunnelInfo.GW,
PeerID: m.tunnelInfo.PeerID,
GW: m.tunnelInfo.GW,
IP: m.tunnelInfo.IP,
MTU: m.tunnelInfo.MTU,
NetMask: m.tunnelInfo.NetMask,
PeerID: m.tunnelInfo.PeerID,
}
}
11 changes: 5 additions & 6 deletions internal/tun/tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tun
import (
"bytes"
"context"
"errors"
"net"
"os"
"sync"
Expand All @@ -15,10 +14,6 @@ import (
"github.com/ooni/minivpn/internal/session"
)

var (
ErrInitializationTimeout = errors.New("timeout while waiting for TUN to start")
)

// StartTUN initializes and starts the TUN device over the vpn.
// If the passed context expires before the TUN device is ready,
func StartTUN(ctx context.Context, conn networkio.FramingConn, options *model.Options) (*TUN, error) {
Expand All @@ -44,7 +39,7 @@ func StartTUN(ctx context.Context, conn networkio.FramingConn, options *model.Op

select {
case <-ctx.Done():
return nil, ErrInitializationTimeout
return nil, ctx.Err()
case <-sessionManager.Ready:
return tunnel, nil
}
Expand Down Expand Up @@ -206,3 +201,7 @@ func (t *tunBioAddr) Network() string {
func (t *tunBioAddr) String() string {
return t.addr
}

func (t *TUN) NetMask() net.IPMask {
return net.IPMask(net.ParseIP(t.session.TunnelInfo().NetMask))
}