-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.go
More file actions
173 lines (148 loc) · 3.65 KB
/
server.go
File metadata and controls
173 lines (148 loc) · 3.65 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"encoding/json"
"github.com/bsm/openrtb"
"github.com/labstack/echo"
"io/ioutil"
"log"
"net/http"
"net/url"
"xojoc.pw/useragent"
)
var GEO_SERVICE = "https://ipinfo.io/"
type Telemetry struct {
OS string `json:"os,omitempty"`
Device string `json:"device,omitempty"`
Client string `json:"client,omitempty"`
State string `json:"state,omitempty"`
Domain string `json:"domain,omitempty"`
}
type GeoInfo struct {
IP string `json:"ip" xml:"ip" form:"ip" query:"ip"`
Hostname string `json:"hostname" xml:"hostname" form:"hostname" query:"hostname"`
City string `json:"city" xml:"city" form:"city" query:"city"`
Region string `json:"region" xml:"region" form:"region" query:"region"`
Country string `json:"country" xml:"country" form:"country" query:"country"`
Location string `json:"loc" xml:"loc" form:"loc" query:"loc"`
Postal string `json:"postal" xml:"postal" form:"postal" query:"postal"`
Org string `json:"org" xml:"org" form:"org" query:"org"`
}
func main() {
e := echo.New()
e.POST("/", getBidAction)
e.Logger.Fatal(e.Start(":1323"))
}
func getBidAction (c echo.Context) error {
var req *openrtb.BidRequest
err := json.NewDecoder(c.Request().Body).Decode(&req)
if err != nil {
return err
}
t := new(Telemetry)
fillFromUA(t, req)
fillDomain(t, req)
fillState(t, req)
/**
* We are returning a Telemetry struct JSON-encoded response
* though in a production project we have to return a valid BidResponse
* @see RTB docs
*/
return c.JSON(http.StatusCreated, t)
}
func fillDomain(t *Telemetry, req *openrtb.BidRequest) {
if req.Site == nil {
return
}
if len(req.Site.Domain) <= 0 {
t.Domain = getDomainFromUrl(&req.Site.Page)
return
}
t.Domain = req.Site.Domain
}
func getDomainFromUrl(urltext *string) string {
u, err := url.Parse(*urltext)
if err != nil {
return ""
}
return u.Hostname()
}
func fillFromUA(t *Telemetry, req *openrtb.BidRequest) {
ua := getUserAgent(req)
if ua == nil {
return
}
fillOperatingSystem(t, ua)
fillClient(t, ua)
fillClientType(t, ua)
}
func getUserAgent(req *openrtb.BidRequest) *useragent.UserAgent {
return useragent.Parse(req.Device.UA)
}
func fillOperatingSystem(t *Telemetry, ua *useragent.UserAgent) {
t.OS = ua.OS;
}
func fillClient(t *Telemetry, ua *useragent.UserAgent) {
t.Client = ua.Name;
}
func fillClientType(t *Telemetry, ua *useragent.UserAgent) {
switch {
case ua.Mobile:
t.Device = "Mobile"
case ua.Tablet:
t.Device = "Tablet"
default:
t.Device = "Desktop"
}
}
func fillState(t *Telemetry, req *openrtb.BidRequest) {
geoInfo := getGeoIpInfo(req)
if geoInfo != nil {
t.State = geoInfo.Country
}
}
func getGeoIpInfo(req *openrtb.BidRequest) *GeoInfo {
serviceUrl := getGeoServiceUrl(req.Device)
if len(serviceUrl) <= 0 {
return nil
}
body := getGeoResponse(&serviceUrl)
if body == nil {
return nil
}
geoInfo := parseGeoInfo(body)
if geoInfo == nil {
return nil
}
return geoInfo
}
func getGeoResponse(url *string) []byte {
resp, err := http.Get(*url)
if err != nil {
log.Printf("%+v\n", "ERROR REQUESTING GEOIP INFO")
return nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
return body
}
func parseGeoInfo(body []byte) *GeoInfo {
geoInfo := new (GeoInfo)
if err := json.Unmarshal(body, &geoInfo); err != nil {
log.Printf("%+v\n", "ERROR PARSING GEOIP INFO")
return nil
}
return geoInfo
}
func getGeoServiceUrl(d *openrtb.Device) string {
switch {
case len(d.IP) > 0:
return getGeoServiceBaseUrl(&d.IP)
case len(d.IPv6) > 0:
return getGeoServiceBaseUrl(&d.IPv6)
default:
return ""
}
}
func getGeoServiceBaseUrl(IP *string) string {
return GEO_SERVICE + *IP
}