Skip to content

Commit

Permalink
binary changes used in calibration
Browse files Browse the repository at this point in the history
  • Loading branch information
ainghazal committed Jan 30, 2024
1 parent 8b025e1 commit 1a0c460
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 10 deletions.
79 changes: 79 additions & 0 deletions cmd/minivpn2/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"fmt"
"io"
"os"
"sync"
"time"

"github.com/apex/log"
)

// Default handler outputting to stderr.
var Default = NewHandler(os.Stderr)

// start time.
var start = time.Now()

// colors.
const (
none = 0
red = 31
green = 32
yellow = 33
blue = 34
gray = 37
)

// Colors mapping.
var Colors = [...]int{
log.DebugLevel: gray,
log.InfoLevel: blue,
log.WarnLevel: yellow,
log.ErrorLevel: red,
log.FatalLevel: red,
}

// Strings mapping.
var Strings = [...]string{
log.DebugLevel: "DEBUG",
log.InfoLevel: "INFO",
log.WarnLevel: "WARN",
log.ErrorLevel: "ERROR",
log.FatalLevel: "FATAL",
}

// Handler implementation.
type Handler struct {
mu sync.Mutex
Writer io.Writer
}

// New handler.
func NewHandler(w io.Writer) *Handler {
return &Handler{
Writer: w,
}
}

// HandleLog implements log.Handler.
func (h *Handler) HandleLog(e *log.Entry) error {
color := Colors[e.Level]
level := Strings[e.Level]
names := e.Fields.Names()

h.mu.Lock()
defer h.mu.Unlock()

ts := time.Since(start)
fmt.Fprintf(h.Writer, "\033[%dm%6s\033[0m[%10v] %-25s", color, level, ts, e.Message)

for _, name := range names {
fmt.Fprintf(h.Writer, " \033[%dm%s\033[0m=%v", color, name, e.Fields.Get(name))
}

fmt.Fprintln(h.Writer)

return nil
}
54 changes: 44 additions & 10 deletions cmd/minivpn2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package main

import (
"context"
"flag"
"fmt"
"net"
"os"
"os/exec"
"time"

"github.com/Doridian/water"
"github.com/apex/log"
"github.com/jackpal/gateway"

"github.com/ooni/minivpn/internal/model"
"github.com/ooni/minivpn/internal/networkio"
"github.com/ooni/minivpn/internal/tun"

"github.com/Doridian/water"
"github.com/jackpal/gateway"
)

func runCmd(binaryPath string, args ...string) {
Expand All @@ -35,11 +37,28 @@ func runRoute(args ...string) {
runCmd("/sbin/route", args...)
}

type config struct {
skipRoute bool
configPath string
timeout int
}

func main() {
log.SetLevel(log.DebugLevel)

cfg := &config{}
flag.BoolVar(&cfg.skipRoute, "skip-route", false, "if true, exists without setting routes (for testing)")
flag.StringVar(&cfg.configPath, "config", "", "config file to load")
flag.IntVar(&cfg.timeout, "timeout", 60, "timeout in seconds (default=60)")
flag.Parse()

if cfg.configPath == "" {
fmt.Println("[error] need config path")
os.Exit(1)
}

// parse the configuration file
options, err := model.ReadConfigFile(os.Args[1])
options, err := model.ReadConfigFile(cfg.configPath)
if err != nil {
log.WithError(err).Fatal("NewOptionsFromFilePath")
}
Expand All @@ -49,28 +68,43 @@ func main() {
if !options.HasAuthInfo() {
log.Fatal("options are missing auth info")
}

log.SetHandler(NewHandler(os.Stderr))
log.SetLevel(log.DebugLevel)

start := time.Now()

// connect to the server
dialer := networkio.NewDialer(log.Log, &net.Dialer{})
ctx := context.Background()

endpoint := net.JoinHostPort(options.Remote, options.Port)

conn, err := dialer.DialContext(ctx, options.Proto.String(), endpoint)
if err != nil {
log.WithError(err).Fatal("dialer.DialContext")
}

// The TLS will expire in 60 seconds by default, but we can pass
// a shorter timeout.
//ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
//defer cancel()
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(cfg.timeout)*time.Second)
defer cancel()

// create a vpn tun Device
tunnel, err := tun.StartTUN(ctx, conn, options)
tunnel, err := tun.StartTUN(ctx, conn, options, log.Log)
if err != nil {
log.WithError(err).Fatal("init error")
return
}
fmt.Printf("Local IP: %s\n", tunnel.LocalAddr())
fmt.Printf("Gateway: %s\n", tunnel.RemoteAddr())
log.Infof("Local IP: %s\n", tunnel.LocalAddr())
log.Infof("Gateway: %s\n", tunnel.RemoteAddr())

fmt.Println("initialization-sequence-completed")
fmt.Printf("elapsed: %v\n", time.Since(start))

if cfg.skipRoute {
os.Exit(0)
}

// create a tun interface on the OS
iface, err := water.New(water.Config{
Expand All @@ -88,7 +122,7 @@ func main() {
remoteAddr := tunnel.RemoteAddr().String()
netMask := tunnel.NetMask()

// discover local gateway IP, to
// discover local gateway IP, we need it to add a route to our remote via our network gw
defaultGatewayIP, err := gateway.DiscoverGateway()
if err != nil {
log.Warn("could not discover default gateway IP, routes might be broken")
Expand Down

0 comments on commit 1a0c460

Please sign in to comment.