forked from ooni/probe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
78 lines (66 loc) · 2.64 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"context"
"net/url"
"os"
"path/filepath"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/engine"
"github.com/ooni/probe-cli/v3/internal/kvstore"
"github.com/ooni/probe-cli/v3/internal/legacy/kvstore2dir"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
// newSessionOrPanic creates and starts a new session or panics on failure
func newSessionOrPanic(ctx context.Context, currentOptions *Options,
miniooniDir string, logger model.Logger) *engine.Session {
var proxyURL *url.URL
if currentOptions.Proxy != "" {
proxyURL = mustParseURL(currentOptions.Proxy)
}
// We renamed kvstore2 to engine in the 3.20 development cycle
_ = kvstore2dir.Move(miniooniDir)
enginedir := filepath.Join(miniooniDir, "engine")
kvstore, err := kvstore.NewFS(enginedir)
runtimex.PanicOnError(err, "cannot create engine directory")
tunnelDir := filepath.Join(miniooniDir, "tunnel")
err = os.MkdirAll(tunnelDir, 0700)
runtimex.PanicOnError(err, "cannot create tunnelDir")
config := engine.SessionConfig{
KVStore: kvstore,
Logger: logger,
ProxyURL: proxyURL,
SnowflakeRendezvous: currentOptions.SnowflakeRendezvous,
SoftwareName: currentOptions.SoftwareName,
SoftwareVersion: currentOptions.SoftwareVersion,
TorArgs: currentOptions.TorArgs,
TorBinary: currentOptions.TorBinary,
TunnelDir: tunnelDir,
}
if currentOptions.ProbeServicesURL != "" {
config.AvailableProbeServices = []model.OOAPIService{{
Address: currentOptions.ProbeServicesURL,
Type: "https",
}}
}
sess, err := engine.NewSession(ctx, config)
runtimex.PanicOnError(err, "cannot create measurement session")
log.Debugf("miniooni temporary directory: %s", sess.TempDir())
return sess
}
func lookupBackendsOrPanic(ctx context.Context, sess *engine.Session) {
log.Info("Looking up OONI backends; please be patient...")
err := sess.MaybeLookupBackendsContext(ctx)
runtimex.PanicOnError(err, "cannot lookup OONI backends")
}
func lookupLocationOrPanic(ctx context.Context, sess *engine.Session) {
log.Info("Looking up your location; please be patient...")
err := sess.MaybeLookupLocationContext(ctx)
runtimex.PanicOnError(err, "cannot lookup your location")
log.Debugf("- IP: %s", sess.ProbeIP()) // make sure it does not appear in default logs
log.Infof("- country: %s", sess.ProbeCC())
log.Infof("- network: %s (%s)", sess.ProbeNetworkName(), sess.ProbeASNString())
log.Infof("- resolver's IP: %s", sess.ResolverIP())
log.Infof("- resolver's network: %s (%s)", sess.ResolverNetworkName(),
sess.ResolverASNString())
}