Skip to content

add a command to set/query a task's remote ip:port information #346

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

Merged
merged 1 commit into from
Apr 22, 2025
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
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ for example:

---

### 📃 <u>re</u>adme
### 📃 <u>rea</u>dme

```
$ ret readme
Expand All @@ -384,6 +384,24 @@ make the readme with ret

---

### 📡 <u>rem</u>ote

```
$ ret remote [ip=127.0.0.1] [port=9001]
```

set or query a task's ip and port with ret

supply no arguments to see the current ip and port

note that task metadata is stored in hidden directory `.ret` and therefore scoped to the cwd

task metadata is stored in the `.ret/task.json` file

🔗 https://github.com/rerrorctf/ret/blob/main/commands/remote.go

---

### 🚮 <u>rm</u>ctf

```
Expand Down
75 changes: 75 additions & 0 deletions commands/remote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package commands

import (
"fmt"
"ret/config"
"ret/theme"
"ret/util"
)

func init() {
Commands = append(Commands, Command{
Name: "remote",
Emoji: "📡",
Func: Remote,
Help: RemoteHelp,
Arguments: []Argument{
{
Name: "ip",
Optional: true,
List: false,
Default: "127.0.0.1",
},
{
Name: "port",
Optional: true,
List: false,
Default: "9001",
},
},
SeeAlso: nil})
}

func RemoteHelp() string {
return "set or query a task's ip and port with ret\n\n" +
"supply no arguments to see the current ip and port\n\n" +
"note that task metadata is stored in hidden directory " + theme.ColorCyan + "`.ret`" + theme.ColorReset + " and therefore scoped to the cwd\n\n" +
"task metadata is stored in the " + theme.ColorCyan + "`" + config.TaskFileName + "`" + theme.ColorReset + " file\n"
}

func displayCurrentTaskIpAndPort() {
ip := util.GetCurrentTaskIp()
port := util.GetCurrentTaskPort()

fmt.Printf("📡 "+theme.ColorBlue+"%s:%d"+theme.ColorReset+"\n", ip, port)
}

func setCurrentTaskIpAndPort(newIp string, newPort int) {
oldIp := util.GetCurrentTaskIp()
oldPort := util.GetCurrentTaskPort()

if (oldIp != newIp) || (oldPort != newPort) {
fmt.Printf(theme.ColorGray+"📡 changing ip:port from: "+theme.ColorRed+"%s:%d"+theme.ColorGray+" to: "+theme.ColorGreen+"%s:%d"+theme.ColorReset+"\n",
oldIp, oldPort, newIp, newPort)
} else {
fmt.Printf(theme.ColorGray+"📡 setting ip:port to: "+theme.ColorGreen+"%s:%d"+theme.ColorReset+"\n", newIp, newPort)
}

util.SetCurrentTaskIp(newIp)
util.SetCurrentTaskPort(newPort)
}

func Remote(args []string) {
if len(args) == 0 {
displayCurrentTaskIpAndPort()
return
}

util.EnsureSkeleton()

var ip string
var port int
util.GetRemoteParams(args, &ip, &port)

setCurrentTaskIpAndPort(ip, port)
}
2 changes: 2 additions & 0 deletions data/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ type Task struct {
Name string `json:"name"`
Category string `json:"category"`
Description string `json:"description"`
Ip string `json:"ip"`
Port int `json:"port"`
Flag string `json:"flag"`
}
24 changes: 24 additions & 0 deletions util/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

func GetCurrentTask() data.Task {
var task data.Task
task.Ip = "127.0.0.1"
task.Port = 9001

jsonData, err := os.ReadFile(config.TaskFileName)
if err != nil {
Expand Down Expand Up @@ -80,3 +82,25 @@ func SetCurrentTaskFlag(flag string) {
task.Flag = flag
SetCurrentTask(&task)
}

func GetCurrentTaskIp() string {
task := GetCurrentTask()
return task.Ip
}

func SetCurrentTaskIp(ip string) {
task := GetCurrentTask()
task.Ip = ip
SetCurrentTask(&task)
}

func GetCurrentTaskPort() int {
task := GetCurrentTask()
return task.Port
}

func SetCurrentTaskPort(port int) {
task := GetCurrentTask()
task.Port = port
SetCurrentTask(&task)
}