Files
go-i2pcontrol/auth.go

68 lines
1.8 KiB
Go
Raw Normal View History

2018-04-03 00:55:29 -04:00
package i2pcontrol
import (
"fmt"
"net/rpc"
)
2018-04-03 00:55:29 -04:00
type i2pControlStructure struct {
jsonstructure *jsonStructure
i2pcontrolhost string
i2pcontrolport string
i2pcontroltoken string
i2pcontrolclient *rpc.Client
i2pcontrolerr error
2018-04-03 00:55:29 -04:00
}
func (i *i2pControlStructure) i2pControlHost() string{
return i.i2pcontrolhost
}
func (i *i2pControlStructure) i2pControlPort() string{
return i.i2pcontrolport
}
func (i *i2pControlStructure) i2pControlDo(method string, params ...string) string{
reply := ""
i.i2pcontrolerr = i.i2pcontrolclient.Call(method, params, &reply)
if i.i2pcontrolerr != nil {
fmt.Println("reply error")
}
return reply
2018-04-03 02:11:46 -04:00
}
func (i *i2pControlStructure) Authenticate(v, pw string) (string, string) {
query := i.jsonstructure.Authenticate("API" , v, "Password", pw)
2018-04-03 02:54:50 -04:00
fmt.Println(query)
response := i.i2pControlDo("Authenticate", pw)
2018-04-03 02:54:50 -04:00
return query, response
}
func (i *i2pControlStructure) Echo(e string) (string, string) {
if i.i2pcontroltoken == "" {
return "", "i2pControl Auth Token not present."
}
query := i.jsonstructure.Echo("Token", i.i2pcontroltoken, "Echo", e)
2018-04-03 02:11:46 -04:00
fmt.Println(query)
response := i.i2pControlDo("Echo", e)
2018-04-03 02:11:46 -04:00
return query, response
}
2018-04-03 00:55:29 -04:00
func (i *i2pControlStructure) i2pControl() i2pControlStructure {
return *i
}
2018-04-03 01:02:04 -04:00
func NewI2pControl(hostport ...string) *i2pControlStructure {
2018-04-03 00:55:29 -04:00
var i i2pControlStructure
if hostport != nil {
if len(hostport) > 0 {
i.i2pcontrolhost = hostport[0]
}else if len(hostport) > 1 {
i.i2pcontrolport = hostport[1]
}
}
i.i2pcontroltoken = ""
i.i2pcontrolclient, i.i2pcontrolerr = rpc.DialHTTP("tcp", i.i2pcontrolhost +":"+ i.i2pcontrolport )
2018-04-03 01:09:53 -04:00
i.jsonstructure = NewJsonStructure()
2018-04-03 00:55:29 -04:00
return &i
}