-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.go
177 lines (161 loc) · 4.21 KB
/
server.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
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
174
175
176
177
package main
import (
"log"
"fmt"
"regexp"
"strings"
"net/http"
"io/ioutil"
"github.com/gosimple/conf"
"github.com/noahm/rss-proxy/rss"
)
var client *http.Client
var config *conf.Config
var pathPrefix string
const FILTER_INCLUDE = 1
const FILTER_EXCLUDE = 2
type Filter struct {
FieldName string
Type int
Pattern *regexp.Regexp
}
type Feed struct {
Name string
url string
username string
password string
agent string
filter *Filter
}
func NewFeed(name, url, username, password, agent string) *Feed {
f := &Feed{
Name: name,
url: url,
username: username,
password: password,
agent: agent,
filter: nil,
}
echo("Registering handler for "+pathPrefix+name)
http.Handle(pathPrefix+name, f)
return f
}
func (f *Feed)AddFilter(t int, field string, pattern string) {
f.filter = &Filter{
FieldName: field,
Type: t,
Pattern: regexp.MustCompile(pattern),
}
}
func (f *Feed)ServeHTTP(respWriter http.ResponseWriter, req *http.Request) {
echo("Proxying "+f.Name)
// request feed from remote
feedReq, _ := http.NewRequest("GET", f.url, nil)
if (f.username != "") {
feedReq.SetBasicAuth(f.username, f.password)
}
if (f.agent != "") {
feedReq.Header.Set("User-Agent", f.agent)
}
feedResp, _ := client.Do(feedReq)
defer feedResp.Body.Close()
// copy headers
for field, values := range feedResp.Header {
if (strings.ToLower(field) != "content-length") {
for _, value := range values {
respWriter.Header().Add(field, value)
}
}
}
if (feedResp.StatusCode != 200 || f.filter == nil) {
// copy feed content without parsing
respWriter.WriteHeader(feedResp.StatusCode)
buf, _ := ioutil.ReadAll(feedResp.Body)
respWriter.Write(buf)
} else {
// parse and filter feed content
feed, err := rss.ParseFromReader(feedResp.Body)
if err != nil {
echo("Error parsing feed: " + err.Error())
respWriter.WriteHeader(http.StatusInternalServerError)
return
}
selectedItems := []rss.Item{}
for _, item := range feed.Channel.Items {
match := f.filter.Pattern.MatchString(item.GetField(f.filter.FieldName))
if ( match && f.filter.Type == FILTER_INCLUDE) ||
(!match && f.filter.Type == FILTER_EXCLUDE) {
selectedItems = append(selectedItems, item)
}
}
feed.Channel.Items = selectedItems
buf, _ := feed.ToBytes()
respWriter.WriteHeader(feedResp.StatusCode)
respWriter.Write(buf)
}
}
func unknownFeed(respWriter http.ResponseWriter, req *http.Request) {
echo("Unknown feed requested")
respWriter.WriteHeader(404)
}
func echo(s string) {
fmt.Println(s)
}
func main() {
client = &http.Client{}
config, _ := conf.ReadFile("server.conf")
// handle 404s for unknown feeds
path, _ := config.String("", "path-prefix")
pathPrefix = path+"/"
echo("Handling unknown feeds with path "+pathPrefix)
http.HandleFunc(pathPrefix, unknownFeed)
// read in configured feeds
feeds := make([]*Feed, 0)
var newFeed *Feed
var filterType int
var filterPattern string
for _, section := range config.Sections() {
if section == "default" {
continue
}
feed, _ := config.String(section, "feed")
username, _ := config.String(section, "username")
password, _ := config.String(section, "password")
agent, err := config.String(section, "user-agent")
if (err != nil) {
agent, _ = config.String("", "user-agent")
}
newFeed = NewFeed(
section,
feed,
username,
password,
agent,
)
filterField, _ := config.String(section, "filter-field")
if filterField != "" {
filterPattern, _ = config.String(section, "filter-include")
if filterPattern == "" {
filterPattern, _ = config.String(section, "filter-exclude")
if filterPattern == "" {
panic("No filter-include or filter-exclude patterns include with filter-field directive in section: "+section)
}
filterType = FILTER_EXCLUDE
} else {
filterType = FILTER_INCLUDE
}
newFeed.AddFilter(filterType, filterField, filterPattern)
}
feeds = append(feeds, newFeed)
}
// start server
useSsl, _ := config.Bool("", "use-ssl")
address, _ := config.String("", "serve-address")
if (useSsl) {
cert, _ := config.String("", "ssl-cert")
key, _ := config.String("", "ssl-key")
log.Fatal(http.ListenAndServeTLS(address, cert, key, nil))
} else {
log.Fatal(http.ListenAndServe(address, nil))
}
}