forked from zubairhamed/canopus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
104 lines (86 loc) · 2.71 KB
/
proxy.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
package canopus
import (
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
)
func NullProxyFilter(Message, net.Addr) bool {
return true
}
// The default handler when proxying is disabled
func NullProxyHandler(c CoapServer, msg Message, session Session) {
SendMessage(ProxyingNotSupportedMessage(msg.GetMessageId(), MessageAcknowledgment), session)
}
func COAPProxyHandler(c CoapServer, msg Message, session Session) {
proxyURI := msg.GetOption(OptionProxyURI).StringValue()
parsedURL, err := url.Parse(proxyURI)
if err != nil {
log.Println("Error parsing proxy URI")
SendMessage(BadGatewayMessage(msg.GetMessageId(), MessageAcknowledgment), session)
return
}
clientConn, err := Dial(parsedURL.Host)
msg.RemoveOptions(OptionProxyURI)
req := NewRequestFromMessage(msg).(*CoapRequest)
req.SetRequestURI(parsedURL.RequestURI())
response, err := clientConn.Send(req)
if err != nil {
SendMessage(BadGatewayMessage(msg.GetMessageId(), MessageAcknowledgment), session)
clientConn.Close()
return
}
_, err = SendMessage(response.GetMessage(), session)
if err != nil {
log.Println("Error occured responding to proxy request")
clientConn.Close()
return
}
clientConn.Close()
}
// Handles requests for proxying from CoAP to HTTP
func HTTPProxyHandler(c CoapServer, msg Message, session Session) {
proxyURI := msg.GetOption(OptionProxyURI).StringValue()
requestMethod := msg.GetCode()
client := &http.Client{}
req, err := http.NewRequest(MethodString(CoapCode(msg.GetMethod())), proxyURI, nil)
if err != nil {
SendMessage(BadGatewayMessage(msg.GetMessageId(), MessageAcknowledgment), session)
return
}
etag := msg.GetOption(OptionEtag)
if etag != nil {
req.Header.Add("ETag", etag.StringValue())
}
// TODO: Set timeout handler, and on timeout return 5.04
resp, err := client.Do(req)
if err != nil {
SendMessage(BadGatewayMessage(msg.GetMessageId(), MessageAcknowledgment), session)
return
}
defer resp.Body.Close()
contents, _ := ioutil.ReadAll(resp.Body)
modifiedMsg := msg.(*CoapMessage)
modifiedMsg.SetPayload(NewBytesPayload(contents))
respMsg := NewRequestFromMessage(modifiedMsg)
if requestMethod == Get {
etag := resp.Header.Get("ETag")
if etag != "" {
msg.AddOption(OptionEtag, etag)
}
}
// TODO: Check payload length against Size1 options
if len(respMsg.GetMessage().GetPayload().String()) > MaxPacketSize {
SendMessage(BadGatewayMessage(msg.GetMessageId(), MessageAcknowledgment), session)
return
}
_, err = SendMessage(respMsg.GetMessage(), session)
if err != nil {
println(err.Error())
}
}
// Handles requests for proxying from HTTP to CoAP
func HTTPCOAPProxyHandler(msg *Message, conn *net.UDPConn, addr net.Addr) {
log.Println("HttpCoapProxyHandler Proxy Handler")
}