| 1 | package kasascip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/ |
| 2 | |
| 3 | import ( |
| 4 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/" |
| 6 | "ioscip-go gomod github.com/golang/go/src go1.18 io/" |
| 7 | "netscip-go gomod github.com/golang/go/src go1.18 net/" |
| 8 | "timescip-go gomod github.com/golang/go/src go1.18 time/" |
| 9 | |
| 10 | "github.com/mitchellh/mapstructurescip-go gomod github.com/mitchellh/mapstructure v1.1.2 `github.com/mitchellh/mapstructure`/" |
| 11 | ) |
| 12 | |
| 13 | type KasaClientscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClient# struct { |
| 14 | addrscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClient#addr. string |
| 15 | modelscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClient#model. string |
| 16 | } |
| 17 | |
| 18 | type KasaClientConfigscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClientConfig# struct { |
| 19 | Hostscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClientConfig#Host. string |
| 20 | } |
| 21 | |
| 22 | type KasaRequestContextscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaRequestContext# struct { |
| 23 | ChildIDsscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaRequestContext#ChildIDs. []string `json:"child_ids"` |
| 24 | } |
| 25 | |
| 26 | type RPCResponsescip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/RPCResponse# struct { |
| 27 | ErrCodescip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/RPCResponse#ErrCode. int `json:"err_code"` |
| 28 | } |
| 29 | |
| 30 | func Newscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/New().(c *KasaClientConfigscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClientConfig#) *KasaClientscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClient# { |
| 31 | return &KasaClientscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClient#{ |
| 32 | addrscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClient#addr.: c.Hostscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClientConfig#Host. + ":9999", |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | const keyscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/key. = 171 |
| 37 | |
| 38 | func packIntscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/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 unpackIntscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/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 encryptscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/encrypt().(in []byte) []byte { |
| 57 | length := len(in) |
| 58 | out := make([]byte, length) |
| 59 | |
| 60 | key := keyscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/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 decryptscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/decrypt().(in []byte) []byte { |
| 69 | length := len(in) |
| 70 | out := make([]byte, length) |
| 71 | key := keyscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/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 *KasaClientscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClient#) Requestscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClient#Request().(payload interface{}) ([]byte, error) { |
| 81 | conn, err := netscip-go gomod github.com/golang/go/src go1.18 net/.Dialscip-go gomod github.com/golang/go/src go1.18 net/Dial().("tcp", c.addrscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClient#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.SetWriteDeadlinescip-go gomod github.com/golang/go/src go1.18 net/Conn#SetWriteDeadline().(timescip-go gomod github.com/golang/go/src go1.18 time/.Nowscip-go gomod github.com/golang/go/src go1.18 time/Now().().Addscip-go gomod github.com/golang/go/src go1.18 time/Time#Add().(timescip-go gomod github.com/golang/go/src go1.18 time/.Secondscip-go gomod github.com/golang/go/src go1.18 time/Second.)) |
| 88 | |
| 89 | jpayload, err := jsonscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Marshal().(payload) |
| 90 | if err != nil { |
| 91 | return nil, fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("error marshaling payload: %v", err) |
| 92 | } |
| 93 | |
| 94 | _, err = conn.Writescip-go gomod github.com/golang/go/src go1.18 net/Conn#Write().(packIntscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/packInt().(int32(len(jpayload)))) |
| 95 | if err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 | _, err = conn.Writescip-go gomod github.com/golang/go/src go1.18 net/Conn#Write().(encryptscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/encrypt().(jpayload)) |
| 99 | if err != nil { |
| 100 | return nil, err |
| 101 | } |
| 102 | |
| 103 | conn.SetReadDeadlinescip-go gomod github.com/golang/go/src go1.18 net/Conn#SetReadDeadline().(timescip-go gomod github.com/golang/go/src go1.18 time/.Nowscip-go gomod github.com/golang/go/src go1.18 time/Now().().Addscip-go gomod github.com/golang/go/src go1.18 time/Time#Add().(timescip-go gomod github.com/golang/go/src go1.18 time/.Secondscip-go gomod github.com/golang/go/src go1.18 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.Readscip-go gomod github.com/golang/go/src go1.18 net/Conn#Read().(tmp) |
| 112 | if err != nil { |
| 113 | if err != ioscip-go gomod github.com/golang/go/src go1.18 io/.EOFscip-go gomod github.com/golang/go/src go1.18 io/EOF. { |
| 114 | return nil, err |
| 115 | } |
| 116 | break |
| 117 | } |
| 118 | read += n |
| 119 | if length == 0 { |
| 120 | length = int(unpackIntscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/unpackInt().(tmp[:4])) |
| 121 | tmp = tmp[4:] |
| 122 | n -= 4 |
| 123 | } |
| 124 | buf = append(buf, tmp[:n]...) |
| 125 | |
| 126 | } |
| 127 | err = conn.Closescip-go gomod github.com/golang/go/src go1.18 net/Conn#Close().() |
| 128 | if err != nil { |
| 129 | return nil, err |
| 130 | } |
| 131 | buf = decryptscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/decrypt().(buf) |
| 132 | |
| 133 | return buf, nil |
| 134 | } |
| 135 | |
| 136 | func (c *KasaClientscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClient#) RPCscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClient#RPC().(service string, cmd string, |
| 137 | ctx *KasaRequestContextscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaRequestContext#, payload interface{}, out interface{}) error { |
| 138 | |
| 139 | errmsgPrefix := fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 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.Requestscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClient#Request().(body) |
| 151 | if err != nil { |
| 152 | return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("%s: %v", errmsgPrefix, err) |
| 153 | } |
| 154 | |
| 155 | var outMarshal map[string]map[string]map[string]interface{} |
| 156 | |
| 157 | err = jsonscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Unmarshal().(response, &outMarshal) |
| 158 | if err != nil { |
| 159 | return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("%s: %v", errmsgPrefix, err) |
| 160 | } |
| 161 | |
| 162 | if outMarshal[service] == nil || outMarshal[service][cmd] == nil { |
| 163 | return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("%s: malformed response: %v", errmsgPrefix, outMarshal) |
| 164 | } |
| 165 | var r RPCResponsescip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/RPCResponse# |
| 166 | mapstructurescip-go gomod github.com/mitchellh/mapstructure v1.1.2 `github.com/mitchellh/mapstructure`/.Decodescip-go gomod github.com/mitchellh/mapstructure v1.1.2 `github.com/mitchellh/mapstructure`/Decode().(outMarshal[service][cmd], &r) |
| 167 | if r.ErrCodescip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/RPCResponse#ErrCode. != 0 { |
| 168 | return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("%s: rpc error: %v", errmsgPrefix, outMarshal) |
| 169 | } |
| 170 | |
| 171 | mapstructurescip-go gomod github.com/mitchellh/mapstructure v1.1.2 `github.com/mitchellh/mapstructure`/.Decodescip-go gomod github.com/mitchellh/mapstructure v1.1.2 `github.com/mitchellh/mapstructure`/Decode().(outMarshal[service][cmd], &out) |
| 172 | |
| 173 | return nil |
| 174 | |
| 175 | } |
| 176 | |
| 177 | func (c *KasaClientscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClient#) SystemServicescip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClient#SystemService().(ctx *KasaRequestContextscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaRequestContext#) *KasaClientSystemServicescip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClientSystemService# { |
| 178 | return &KasaClientSystemServicescip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClientSystemService#{ |
| 179 | cscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClientSystemService#c.: c, |
| 180 | ctxscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClientSystemService#ctx.: ctx, |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | func (c *KasaClientscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClient#) EmeterServicescip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClient#EmeterService().(ctx *KasaRequestContextscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaRequestContext#) *KasaClientEmeterServicescip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClientEmeterService# { |
| 185 | return &KasaClientEmeterServicescip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClientEmeterService#{ |
| 186 | cscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClientEmeterService#c.: c, |
| 187 | ctxscip-go gomod github.com/fffonion/tplink-plug-exporter . `github.com/fffonion/tplink-plug-exporter/kasa`/KasaClientEmeterService#ctx.: ctx, |
| 188 | } |
| 189 | } |