Skip to content

Commit 5613564

Browse files
author
Todd Blose
committed
only apply delta deletions that exist in cloud list; otherwise just remove from add set
1 parent cf93029 commit 5613564

5 files changed

Lines changed: 136 additions & 91 deletions

File tree

‎src/github.com/getlantern/flashlight/flashlight.go‎

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,36 @@ func runClientProxy(cfg *config.Config) {
111111
// Configure client initially
112112
client.Configure(cfg.Client)
113113

114-
// this flag specifies the port to open the HTTP server
115-
// between the UI and
116-
// with a corresponding HTTP server at
117-
// the following address
118-
runUIServer(cfg)
114+
go func() {
115+
for {
116+
cfg := <-configUpdates
117+
log.Debugf("config changed to %+v", cfg.Client.ProxiedSites.Additions)
118+
ps := proxiedsites.Configure(cfg.Client.ProxiedSites)
119+
// this flag specifies the port to open the HTTP server
120+
// between the UI and
121+
// with a corresponding HTTP server at
122+
// the following address
123+
if cfg.UIAddr != "" {
124+
// Configure the UI Server
125+
go http.ConfigureUIServer(cfg.UIAddr, cfg.OpenUI, ps)
126+
}
127+
128+
go func() {
129+
for {
130+
cfg.Client.ProxiedSites = <-ps.Updates
131+
err := config.Update(func(updated *config.Config) error {
132+
log.Debugf("Saving updated proxiedsites configuration")
133+
updated.Client.ProxiedSites = cfg.Client.ProxiedSites
134+
return nil
135+
})
136+
137+
if err != nil {
138+
log.Errorf("Could not update config: %s", err)
139+
}
140+
}
141+
}()
142+
}
143+
}()
119144

120145
// Continually poll for config updates and update client accordingly
121146
go func() {
@@ -133,44 +158,25 @@ func runClientProxy(cfg *config.Config) {
133158
}
134159

135160
// Runs the UI Server
136-
func runUIServer(cfg *config.Config) {
137-
if cfg.UIAddr != "" {
138-
proxiedSitesChan := make(chan *proxiedsites.ProxiedSites)
139-
go func() {
140-
for {
141-
cfg := <-configUpdates
142-
ps := proxiedsites.New(cfg.Client.ProxiedSites)
143-
srv := &http.UIServer{
144-
ProxiedSites: ps,
145-
ProxiedSitesChan: proxiedSitesChan,
146-
Addr: cfg.UIAddr,
147-
ConfigUpdates: configUpdates,
148-
}
149-
srv.StartServer()
150-
log.Debug("Started UI server...")
151-
}
152-
}()
153-
// if the openui flag is specified, the UI is automatically
154-
// opened in the default browser on start
155-
http.OpenUI(cfg.OpenUI, cfg.UIAddr)
156-
157-
go func() {
158-
for {
159-
// separate channel for synchronizing proxiedsites updates
160-
ps := <-proxiedSitesChan
161-
cfg.Client.ProxiedSites = ps.GetConfig()
162-
err := config.Update(func(updated *config.Config) error {
163-
log.Debugf("Saving updated proxiedsites configuration")
164-
updated.Client.ProxiedSites = cfg.Client.ProxiedSites
165-
return nil
166-
})
167-
168-
if err != nil {
169-
log.Errorf("Could not update config: %s", err)
170-
}
161+
func runUIServer(cfg *config.Config, ps *proxiedsites.ProxiedSites) {
162+
http.ConfigureUIServer(cfg.UIAddr, cfg.OpenUI, ps)
163+
// if the openui flag is specified, the UI is automatically
164+
// opened in the default browser on start
165+
/* go func() {
166+
for {
167+
ps := <-proxiedSitesChan
168+
cfg.Client.ProxiedSites = ps.GetConfig()
169+
err := config.Update(func(updated *config.Config) error {
170+
log.Debugf("Saving updated proxiedsites configuration")
171+
updated.Client.ProxiedSites = cfg.Client.ProxiedSites
172+
return nil
173+
})
174+
175+
if err != nil {
176+
log.Errorf("Could not update config: %s", err)
171177
}
172-
}()
173-
}
178+
}
179+
}()*/
174180

175181
}
176182

‎src/github.com/getlantern/flashlight/http/http.go‎

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import (
44
"fmt"
55
"net/http"
66
"path"
7-
"reflect"
87
"runtime"
8+
"sync"
99
"time"
1010

11-
"github.com/getlantern/flashlight/config"
1211
"github.com/getlantern/flashlight/util"
1312
"github.com/getlantern/golog"
1413
"github.com/getlantern/proxiedsites"
@@ -30,6 +29,8 @@ var (
3029
log = golog.LoggerFor("http")
3130
upgrader = &websocket.Upgrader{ReadBufferSize: 1024, WriteBufferSize: MaxMessageSize}
3231
UIDir string
32+
instance *UIServer
33+
mutex sync.Mutex
3334
)
3435

3536
// represents a UI client
@@ -43,14 +44,12 @@ type Client struct {
4344

4445
type UIServer struct {
4546
connections map[*Client]bool // pool of UI client connections
46-
Addr string
47+
addr string
4748
// current set of proxied sites
48-
ProxiedSites *proxiedsites.ProxiedSites
49+
proxiedSites *proxiedsites.ProxiedSites
4950

50-
requests chan *Client
51-
connClose chan *Client // handles client disconnects
52-
ProxiedSitesChan chan *proxiedsites.ProxiedSites
53-
ConfigUpdates chan *config.Config
51+
requests chan *Client
52+
connClose chan *Client // handles client disconnects
5453
}
5554

5655
// Assume the default directory containing UI assets is
@@ -98,7 +97,7 @@ func serveHome(r *http.ServeMux) {
9897
// chunks messages according to WriteBufferSize
9998
func (srv UIServer) writeGlobalList(client *Client) {
10099
initMsg := proxiedsites.Config{
101-
Additions: srv.ProxiedSites.GetEntries(),
100+
Additions: srv.proxiedSites.GetEntries(),
102101
}
103102
// write the JSON encoding of the proxied sites to the
104103
// websocket connection
@@ -137,15 +136,14 @@ func (srv UIServer) readClientMessage(client *Client) {
137136
if err != nil {
138137
break
139138
}
140-
srv.ProxiedSites.Update(&updates)
141-
srv.ProxiedSitesChan <- srv.ProxiedSites
139+
srv.proxiedSites.Update(&updates)
140+
srv.proxiedSites.Updates <- srv.proxiedSites.GetConfig()
142141
}
143142
}
144143

145144
// if the openui flag is specified, the UI is automatically
146145
// opened in the default browser
147146
func OpenUI(shouldOpen bool, uiAddr string) {
148-
149147
uiAddr = fmt.Sprintf(UIUrl, uiAddr)
150148
if shouldOpen {
151149
err := open.Run(uiAddr)
@@ -179,35 +177,26 @@ func (srv UIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
179177
func (srv UIServer) processRequests() {
180178
for {
181179
select {
182-
// wait for YAML config updates
183-
case cfg := <-srv.ConfigUpdates:
184-
185-
if reflect.DeepEqual(cfg.Client.ProxiedSites, srv.ProxiedSites.GetConfig()) {
186-
// ignore conifg update if proxied sites list is unchanged
187-
continue
188-
}
189-
log.Debugf("Proxied sites updated in config file; applying changes..")
190-
newPs := proxiedsites.New(cfg.Client.ProxiedSites)
191-
diff := srv.ProxiedSites.Diff(newPs)
192-
srv.ProxiedSites = newPs
180+
case diff := <-srv.proxiedSites.CfgUpdates:
193181
// write proxied sites update to every UI instance
194182
for c := range srv.connections {
195183
select {
196-
// write the JSON encoding of the proxied sites to the
197-
// websocket connections
184+
// write the JSON encoding of the proxied sites changes
185+
// to the UI clients
198186
case c.msg <- diff:
199187
default:
188+
// close and remove unresponsive connections
200189
close(c.msg)
201190
delete(srv.connections, c)
202191
}
203192
}
204193
case c := <-srv.requests:
205-
log.Debug("Adding new UI instance..")
194+
log.Debug("New UI client connected..")
206195
srv.connections[c] = true
207196
// write initial proxied sites list
208197
srv.writeGlobalList(c)
209198
case c := <-srv.connClose:
210-
log.Debug("Disconnecting UI instance..")
199+
log.Debug("UI client disconnected..")
211200
if _, ok := srv.connections[c]; ok {
212201
delete(srv.connections, c)
213202
close(c.msg)
@@ -216,28 +205,45 @@ func (srv UIServer) processRequests() {
216205
}
217206
}
218207

219-
func (srv UIServer) StartServer() {
208+
func ConfigureUIServer(UIAddr string, openUI bool,
209+
proxiedSites *proxiedsites.ProxiedSites) {
220210

221-
r := http.NewServeMux()
211+
mutex.Lock()
212+
defer mutex.Unlock()
213+
214+
if instance != nil {
215+
instance.proxiedSites = proxiedSites
216+
return
217+
}
222218

223219
// initial request, connection close channels and
224220
// connection pool for this UI server
225-
srv.connClose = make(chan *Client)
226-
srv.requests = make(chan *Client)
227-
srv.connections = make(map[*Client]bool)
221+
srv := &UIServer{
222+
addr: UIAddr,
223+
proxiedSites: proxiedSites,
224+
connClose: make(chan *Client),
225+
requests: make(chan *Client),
226+
connections: make(map[*Client]bool),
227+
}
228+
instance = srv
228229

229-
go srv.processRequests()
230+
go instance.processRequests()
230231

232+
r := http.NewServeMux()
231233
r.Handle("/data", srv)
232234
r.HandleFunc("/proxy_on.pac", servePacFile)
233235
serveHome(r)
234236

235-
log.Debugf("Starting UI HTTP server at %s", srv.Addr)
237+
log.Debugf("Starting UI HTTP server at %s", instance.addr)
236238
httpServer := &http.Server{
237-
Addr: srv.Addr,
239+
Addr: instance.addr,
238240
Handler: r,
239241
}
240242

243+
// if the openui flag is specified, the UI is automatically
244+
// opened in the default browser on start
245+
OpenUI(openUI, UIAddr)
246+
241247
// Run the UI websocket server asynchronously
242-
go log.Fatal(httpServer.ListenAndServe())
248+
log.Fatal(httpServer.ListenAndServe())
243249
}

‎src/github.com/getlantern/proxiedsites/proxiedsites.go‎

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var (
2929
ConfigDir string
3030
PacFilePath string
3131
PacTmpl = "src/github.com/getlantern/proxiedsites/templates/proxy_on.pac.template"
32+
instance *ProxiedSites
3233
)
3334

3435
type Config struct {
@@ -50,6 +51,9 @@ type ProxiedSites struct {
5051

5152
entries []string
5253
pacFile *PacFile
54+
55+
Updates chan *Config
56+
CfgUpdates chan *Config
5357
}
5458

5559
type PacFile struct {
@@ -70,7 +74,30 @@ func init() {
7074
PacFilePath = path.Join(ConfigDir, PacFilename)
7175
}
7276

77+
func Configure(cfg *Config) *ProxiedSites {
78+
if instance != nil {
79+
go func() {
80+
newPs := New(cfg)
81+
82+
// send delta adds and dels to clients
83+
diff := instance.Diff(newPs)
84+
instance.CfgUpdates <- diff
85+
86+
instance = newPs
87+
go newPs.updatePacFile()
88+
}()
89+
return instance
90+
}
91+
ps := New(cfg)
92+
93+
instance = ps
94+
95+
go ps.updatePacFile()
96+
return ps
97+
}
98+
7399
func New(cfg *Config) *ProxiedSites {
100+
74101
// initialize our proxied site sets
75102
cloudSet := set.New()
76103
addSet := set.New()
@@ -93,15 +120,14 @@ func New(cfg *Config) *ProxiedSites {
93120
sort.Strings(entries)
94121

95122
ps := &ProxiedSites{
96-
cfg: cfg,
97-
entries: entries,
98-
cloudSet: cloudSet,
99-
addSet: addSet,
100-
delSet: delSet,
123+
cfg: cfg,
124+
entries: entries,
125+
cloudSet: cloudSet,
126+
addSet: addSet,
127+
Updates: make(chan *Config),
128+
CfgUpdates: make(chan *Config),
129+
delSet: delSet,
101130
}
102-
103-
go ps.updatePacFile()
104-
105131
return ps
106132
}
107133

@@ -118,6 +144,7 @@ func (prev *ProxiedSites) Diff(cur *ProxiedSites) *Config {
118144

119145
return &Config{
120146
Additions: additions,
147+
Deletions: set.StringSlice(delSet),
121148
}
122149
}
123150

@@ -164,6 +191,10 @@ func GetPacFile() string {
164191
return PacFilePath
165192
}
166193

194+
func SetPacFile(pacFile string) {
195+
PacFilePath = pacFile
196+
}
197+
167198
func (ps *ProxiedSites) updatePacFile() (err error) {
168199

169200
pacFile := &PacFile{}

‎src/github.com/getlantern/proxiedsites/proxiedsites_test.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ var (
1111
pacFilePath = PacFilename
1212
)
1313

14-
var mockWhitelists = []struct {
15-
entry *Whitelist
14+
var mockProxiedSites = []struct {
15+
entry *ProxiedSites
1616
}{
1717
{
1818
entry: New(&Config{
@@ -42,11 +42,11 @@ func TestPacFileUpdated(t *testing.T) {
4242
PacFilePath = "proxy_on.pac"
4343
PacTmpl = "templates/proxy_on.pac.template"
4444

45-
for _, mock := range mockWhitelists {
45+
for _, mock := range mockProxiedSites {
46+
SetPacFile(PacFilePath)
4647
mockWl := mock.entry
4748
exists, _ := util.FileExists("proxy_on.pac")
4849
assert.Equal(t, exists, true, "proxy pac file could not be created")
49-
mockWl.RefreshEntries()
5050
time.Sleep(1000 * time.Millisecond)
5151
wl := ParsePacFile()
5252
assert.Equal(t, wl.GetEntries(), mockWl.GetEntries(), "Test domains are not equal!")

0 commit comments

Comments
 (0)