Skip to content

Commit c949348

Browse files
authored
Merge pull request #346 from dmur1/add-remote-command-#333
add a command to set/query a task's remote ip:port information
2 parents 9510eb5 + 05d4cad commit c949348

File tree

4 files changed

+120
-1
lines changed

4 files changed

+120
-1
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ for example:
372372

373373
---
374374

375-
### 📃 <u>re</u>adme
375+
### 📃 <u>rea</u>dme
376376

377377
```
378378
$ ret readme
@@ -384,6 +384,24 @@ make the readme with ret
384384

385385
---
386386

387+
### 📡 <u>rem</u>ote
388+
389+
```
390+
$ ret remote [ip=127.0.0.1] [port=9001]
391+
```
392+
393+
set or query a task's ip and port with ret
394+
395+
supply no arguments to see the current ip and port
396+
397+
note that task metadata is stored in hidden directory `.ret` and therefore scoped to the cwd
398+
399+
task metadata is stored in the `.ret/task.json` file
400+
401+
🔗 https://github.com/rerrorctf/ret/blob/main/commands/remote.go
402+
403+
---
404+
387405
### 🚮 <u>rm</u>ctf
388406

389407
```

commands/remote.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"ret/config"
6+
"ret/theme"
7+
"ret/util"
8+
)
9+
10+
func init() {
11+
Commands = append(Commands, Command{
12+
Name: "remote",
13+
Emoji: "📡",
14+
Func: Remote,
15+
Help: RemoteHelp,
16+
Arguments: []Argument{
17+
{
18+
Name: "ip",
19+
Optional: true,
20+
List: false,
21+
Default: "127.0.0.1",
22+
},
23+
{
24+
Name: "port",
25+
Optional: true,
26+
List: false,
27+
Default: "9001",
28+
},
29+
},
30+
SeeAlso: nil})
31+
}
32+
33+
func RemoteHelp() string {
34+
return "set or query a task's ip and port with ret\n\n" +
35+
"supply no arguments to see the current ip and port\n\n" +
36+
"note that task metadata is stored in hidden directory " + theme.ColorCyan + "`.ret`" + theme.ColorReset + " and therefore scoped to the cwd\n\n" +
37+
"task metadata is stored in the " + theme.ColorCyan + "`" + config.TaskFileName + "`" + theme.ColorReset + " file\n"
38+
}
39+
40+
func displayCurrentTaskIpAndPort() {
41+
ip := util.GetCurrentTaskIp()
42+
port := util.GetCurrentTaskPort()
43+
44+
fmt.Printf("📡 "+theme.ColorBlue+"%s:%d"+theme.ColorReset+"\n", ip, port)
45+
}
46+
47+
func setCurrentTaskIpAndPort(newIp string, newPort int) {
48+
oldIp := util.GetCurrentTaskIp()
49+
oldPort := util.GetCurrentTaskPort()
50+
51+
if (oldIp != newIp) || (oldPort != newPort) {
52+
fmt.Printf(theme.ColorGray+"📡 changing ip:port from: "+theme.ColorRed+"%s:%d"+theme.ColorGray+" to: "+theme.ColorGreen+"%s:%d"+theme.ColorReset+"\n",
53+
oldIp, oldPort, newIp, newPort)
54+
} else {
55+
fmt.Printf(theme.ColorGray+"📡 setting ip:port to: "+theme.ColorGreen+"%s:%d"+theme.ColorReset+"\n", newIp, newPort)
56+
}
57+
58+
util.SetCurrentTaskIp(newIp)
59+
util.SetCurrentTaskPort(newPort)
60+
}
61+
62+
func Remote(args []string) {
63+
if len(args) == 0 {
64+
displayCurrentTaskIpAndPort()
65+
return
66+
}
67+
68+
util.EnsureSkeleton()
69+
70+
var ip string
71+
var port int
72+
util.GetRemoteParams(args, &ip, &port)
73+
74+
setCurrentTaskIpAndPort(ip, port)
75+
}

data/task.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ type Task struct {
44
Name string `json:"name"`
55
Category string `json:"category"`
66
Description string `json:"description"`
7+
Ip string `json:"ip"`
8+
Port int `json:"port"`
79
Flag string `json:"flag"`
810
}

util/task.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111

1212
func GetCurrentTask() data.Task {
1313
var task data.Task
14+
task.Ip = "127.0.0.1"
15+
task.Port = 9001
1416

1517
jsonData, err := os.ReadFile(config.TaskFileName)
1618
if err != nil {
@@ -80,3 +82,25 @@ func SetCurrentTaskFlag(flag string) {
8082
task.Flag = flag
8183
SetCurrentTask(&task)
8284
}
85+
86+
func GetCurrentTaskIp() string {
87+
task := GetCurrentTask()
88+
return task.Ip
89+
}
90+
91+
func SetCurrentTaskIp(ip string) {
92+
task := GetCurrentTask()
93+
task.Ip = ip
94+
SetCurrentTask(&task)
95+
}
96+
97+
func GetCurrentTaskPort() int {
98+
task := GetCurrentTask()
99+
return task.Port
100+
}
101+
102+
func SetCurrentTaskPort(port int) {
103+
task := GetCurrentTask()
104+
task.Port = port
105+
SetCurrentTask(&task)
106+
}

0 commit comments

Comments
 (0)