| 1 | package kasa |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "net" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/mitchellh/mapstructure" |
| 11 | ) |
| 12 | |
| 13 | type KasaClient struct { |
| 14 | addr string |
| 15 | model string |
| 16 | } |
| 17 | |
| 18 | type KasaClientConfig struct { |
| 19 | Host string |
| 20 | } |
| 21 | |
| 22 | type KasaRequestContext struct { |
| 23 | ChildIDs []string `json:"child_ids"` |
| 24 | } |
| 25 | |
| 26 | type RPCResponse struct { |
| 27 | ErrCode int `json:"err_code"` |
| 28 | } |
| 29 | |
| 30 | func New(c *KasaClientConfig) *KasaClient { |
| 31 | return &KasaClient{ |
| 32 | addr: c.Host + ":9999", |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | const key = 171 |
| 37 | |
| 38 | func packInt(in int32) []byte { |
| 39 | out := make([]byte, 4) |
| 40 | for i := 3; i > 0; i-- { |
| 41 | out[i] = byte(in & 0xFF) |
| 42 | in >>= 8 |
| 43 | } |
| 44 | return out |
| 45 | } |
| 46 | |
| 47 | func unpackInt(in []byte) int32 { |
| 48 | length := 0 |
| 49 | for i := 0; i < 4; i++ { |
| 50 | length <<= 8 |
| 51 | length += int(in[i]) |
| 52 | } |
| 53 | return int32(length) |
| 54 | } |
| 55 | |
| 56 | func encrypt(in []byte) []byte { |
| 57 | length := len(in) |
| 58 | out := make([]byte, length) |
| 59 | |
| 60 | key := key |
| 61 | for i, r := range in { |
| 62 | key = key ^ int(r) |
| 63 | out[i] = byte(key) |
| 64 | } |
| 65 | return out |
| 66 | } |
| 67 | |
| 68 | func decrypt(in []byte) []byte { |
| 69 | length := len(in) |
| 70 | out := make([]byte, length) |
| 71 | key := key |
| 72 | for i := 0; i < length; i++ { |
| 73 | b := int(in[i]) |
| 74 | out[i] = byte(key ^ b) |
| 75 | key = b |
| 76 | } |
| 77 | return out |
| 78 | } |
| 79 | |
| 80 | func (c *KasaClient) Request(payload interface{}) ([]byte, error) { |
| 81 | conn, err := net.Dial("tcp", c.addr) |
| 82 | if err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | // the tcp server on smart plug can only handle one connection |
| 86 | // at a time, make sure we don't wait too long to block others |
| 87 | conn.SetWriteDeadline(time.Now().Add(time.Second)) |
| 88 | |
| 89 | jpayload, err := json.Marshal(payload) |
| 90 | if err != nil { |
| 91 | return nil, fmt.Errorf("error marshaling payload: %v", err) |
| 92 | } |
| 93 | |
| 94 | _, err = conn.Write(packInt(int32(len(jpayload)))) |
| 95 | if err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 | _, err = conn.Write(encrypt(jpayload)) |
| 99 | if err != nil { |
| 100 | return nil, err |
| 101 | } |
| 102 | |
| 103 | conn.SetReadDeadline(time.Now().Add(time.Second)) |
| 104 | |
| 105 | var buf []byte |
| 106 | tmp := make([]byte, 1024) |
| 107 | |
| 108 | read := -1 |
| 109 | length := 0 |
| 110 | for read < length { |
| 111 | n, err := conn.Read(tmp) |
| 112 | if err != nil { |
| 113 | if err != io.EOF { |
| 114 | return nil, err |
| 115 | } |
| 116 | break |
| 117 | } |
| 118 | read += n |
| 119 | if length == 0 { |
| 120 | length = int(unpackInt(tmp[:4])) |
| 121 | tmp = tmp[4:] |
| 122 | n -= 4 |
| 123 | } |
| 124 | buf = append(buf, tmp[:n]...) |
| 125 | |
| 126 | } |
| 127 | err = conn.Close() |
| 128 | if err != nil { |
| 129 | return nil, err |
| 130 | } |
| 131 | buf = decrypt(buf) |
| 132 | |
| 133 | return buf, nil |
| 134 | } |
| 135 | |
| 136 | func (c *KasaClient) RPC(service string, cmd string, |
| 137 | ctx *KasaRequestContext, payload interface{}, out interface{}) error { |
| 138 | |
| 139 | errmsgPrefix := fmt.Sprintf("%s.%s", service, cmd) |
| 140 | |
| 141 | body := map[string]interface{}{ |
| 142 | service: map[string]interface{}{ |
| 143 | cmd: payload, |
| 144 | }, |
| 145 | } |
| 146 | if ctx != nil { |
| 147 | body["context"] = ctx |
| 148 | } |
| 149 | |
| 150 | response, err := c.Request(body) |
| 151 | if err != nil { |
| 152 | return fmt.Errorf("%s: %v", errmsgPrefix, err) |
| 153 | } |
| 154 | |
| 155 | var outMarshal map[string]map[string]map[string]interface{} |
| 156 | |
| 157 | err = json.Unmarshal(response, &outMarshal) |
| 158 | if err != nil { |
| 159 | return fmt.Errorf("%s: %v", errmsgPrefix, err) |
| 160 | } |
| 161 | |
| 162 | if outMarshal[service] == nil || outMarshal[service][cmd] == nil { |
| 163 | return fmt.Errorf("%s: malformed response: %v", errmsgPrefix, outMarshal) |
| 164 | } |
| 165 | var r RPCResponse |
| 166 | mapstructure.Decode(outMarshal[service][cmd], &r) |
| 167 | if r.ErrCode != 0 { |
| 168 | return fmt.Errorf("%s: rpc error: %v", errmsgPrefix, outMarshal) |
| 169 | } |
| 170 | |
| 171 | mapstructure.Decode(outMarshal[service][cmd], &out) |
| 172 | |
| 173 | return nil |
| 174 | |
| 175 | } |
| 176 | |
| 177 | func (c *KasaClient) SystemService(ctx *KasaRequestContext) *KasaClientSystemService { |
| 178 | return &KasaClientSystemService{ |
| 179 | c: c, |
| 180 | ctx: ctx, |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | func (c *KasaClient) EmeterService(ctx *KasaRequestContext) *KasaClientEmeterService { |
| 185 | return &KasaClientEmeterService{ |
| 186 | c: c, |
| 187 | ctx: ctx, |
| 188 | } |
| 189 | } |