forked from danielpaulus/go-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_setup.go
More file actions
94 lines (80 loc) · 2.21 KB
/
Copy pathcli_setup.go
File metadata and controls
94 lines (80 loc) · 2.21 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"log/slog"
"os"
"github.com/danielpaulus/go-ios/ios"
"github.com/danielpaulus/go-ios/ios/tunnel"
"github.com/docopt/docopt-go"
)
func configureCLI(arguments docopt.Opts) {
disableJSON, _ := arguments.Bool("--nojson")
if disableJSON {
JSONdisabled = true
}
pretty, _ := arguments.Bool("--pretty")
if pretty {
prettyJSON = true
}
traceLevelEnabled, _ := arguments.Bool("--trace")
verboseLoggingEnabledLong, _ := arguments.Bool("--verbose")
level := slog.LevelInfo
if verboseLoggingEnabledLong {
level = slog.LevelDebug
}
if traceLevelEnabled {
level = ios.LevelTrace
}
handlerOpts := &slog.HandlerOptions{
Level: level,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.LevelKey {
if lvl, ok := a.Value.Any().(slog.Level); ok && lvl == ios.LevelTrace {
a.Value = slog.StringValue("TRACE")
}
}
return a
},
}
var handler slog.Handler
if disableJSON {
handler = slog.NewTextHandler(os.Stderr, handlerOpts)
} else {
handler = slog.NewJSONHandler(os.Stderr, handlerOpts)
}
logger := slog.New(handler)
slog.SetDefault(logger)
ios.SetLogger(logger)
if traceLevelEnabled {
slog.Info("Set Trace mode")
} else if verboseLoggingEnabledLong {
slog.Info("Set Debug mode")
}
slog.Debug("parsed arguments", "args", redactArgs(arguments))
startAgentFromEnvironment()
warnIfAgentIsNotRunning()
}
func redactArgs(arguments docopt.Opts) map[string]interface{} {
redacted := make(map[string]interface{}, len(arguments))
for key, value := range arguments {
switch key {
case "--password", "--p12password", "--proxyurl":
if value != nil && value != "" {
redacted[key] = "<redacted>"
continue
}
}
redacted[key] = value
}
return redacted
}
func startAgentFromEnvironment() {
skipAgent, _ := os.LookupEnv("ENABLE_GO_IOS_AGENT")
if skipAgent == "user" || skipAgent == "kernel" {
tunnel.RunAgent(skipAgent)
}
}
func warnIfAgentIsNotRunning() {
if !tunnel.IsAgentRunning() {
slog.Warn("go-ios agent is not running. You might need to start it with 'ios tunnel start' for ios17+. Use ENABLE_GO_IOS_AGENT=user for userspace tunnel or ENABLE_GO_IOS_AGENT=kernel for kernel tunnel for the experimental daemon mode.")
}
}