File: Proxy.tcl

package info (click to toggle)
coccinella 0.96.20-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 13,184 kB
  • sloc: tcl: 124,744; xml: 206; makefile: 66; sh: 62
file content (429 lines) | stat: -rw-r--r-- 11,911 bytes parent folder | download | duplicates (4)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#  Proxy.tcl ---
#  
#       This file is part of The Coccinella application.
#       It is supposed to provide proxy support. 
#      
#  Copyright (c) 2005-2007  Mats Bengtsson
#  
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#   
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#   
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#  
# $Id: Proxy.tcl,v 1.18 2007-09-14 08:11:46 matben Exp $
 
package require autoproxy
package require autosocks

package provide Proxy 1.0

namespace eval ::Proxy {
    
    ::hooks::register prefsInitHook          ::Proxy::InitPrefsHook
}

proc ::Proxy::InitPrefsHook { } {
    global  prefs
    
    variable keySpecs
    array set keySpecs {
	useproxy        0
	proxy_type      http
	proxy_host      ""
	proxy_port      ""
	proxy_user      ""
	proxy_pass      ""
	noproxy         ""
	setNATip        0
	NATip           ""
    }
    
    # Just a dummy initialization.
    foreach {key value} [array get keySpecs] {
	set prefs($key) $value
    }
    
    # Configure and set our defaults.
    # @@@ This will only set HTTP proxy (on windows) but we need it to depend
    #     on proxy type!
    # Note: -basic {Proxy-Authorization {Basic bWF0czp6eno=}}
    autoproxy::init    
    autosocks::init
    foreach {key value} [::autoproxy::configure] {
	switch -- $key {
	    -basic  { 
		# we can't get username & password from this.
	    }
	    -no_proxy - -proxy_port - -proxy_host {
		set name [string trimleft $key "-"]
		set prefs($name) $value
	    }
	}
    }
    
    ::PrefUtils::Add [list  \
      [list prefs(useproxy)       prefs_useproxy       $prefs(useproxy)]      \
      [list prefs(proxy_type)     prefs_proxytype      $prefs(proxy_type)]    \
      [list prefs(proxy_host)     prefs_proxyhost      $prefs(proxy_host)]    \
      [list prefs(proxy_port)     prefs_proxyport      $prefs(proxy_port)]    \
      [list prefs(proxy_user)     prefs_proxyuser      $prefs(proxy_user)]    \
      [list prefs(proxy_pass)     prefs_proxypass      $prefs(proxy_pass)]    \
      [list prefs(noproxy)        prefs_noproxy        $prefs(noproxy)]       \
      [list prefs(setNATip)       prefs_setNATip       $prefs(setNATip)]      \
      [list prefs(NATip)          prefs_NATip          $prefs(NATip)]         \
      ]
    
    # We shall do our http proxy configuration here transparently for any
    # package that uses the http package.
    # Any user settings override the one we get when autoproxy::init. 
    if {$prefs(useproxy)} {
	if {$prefs(proxy_type) eq "http"} {
	    AutoProxyConfig
	} elseif {[string match {socks[45]} $prefs(proxy_type)]} {
	    AutoSocksConfig
	}
    }
}

proc ::Proxy::AutoProxyConfig { } {
    global  prefs
    
    ::autoproxy::configure  \
      -proxy_host $prefs(proxy_host) \
      -proxy_port $prefs(proxy_port) \
      -no_proxy   $prefs(no_proxy)
    
    if {[string length $prefs(proxy_user)]} {
	::autoproxy::configure  \
	  -basic -user $prefs(proxy_user) -pass $prefs(proxy_pass)
    }
}

proc ::Proxy::AutoProxyOff { } {
    ::autoproxy::configure -proxy_host {} -proxy_port {} \
      -basic -user {} -pass {}
}

proc ::Proxy::AutoSocksConfig { } {
    global  prefs
    
    autosocks::config  \
      -proxy     $prefs(proxy_type) \
      -proxyhost $prefs(proxy_host) \
      -proxyport $prefs(proxy_port) \
      -proxyno   $prefs(no_proxy)
    
    if {[string length $prefs(proxy_user)]} {
	autosocks::config  \
	   -proxyusername $prefs(proxy_user) -proxypassword $prefs(proxy_pass)
    }
}

proc ::Proxy::AutoSocksOff { } {
    autosocks::config -proxy {} -proxyno {} -proxyhost {} -proxyport {} \
      -proxyusername {} -proxypassword {}
}

proc ::Proxy::BuildPage {wpage} {
    
    set anchor [option get . dialogAnchor {}]
    
    set wc $wpage.c
    ttk::frame $wc -padding [option get . notebookPageSmallPadding {}]
    pack $wc -side top -anchor $anchor

    # Proxy:
    ttk::labelframe $wc.p -text [mc "Proxy"]  \
      -padding [option get . groupSmallPadding {}]
    pack $wc.p -side top -fill x -anchor $anchor

    BuildFrame $wc.p.f
    pack $wc.p.f -side top -fill x -anchor $anchor

    # NAT:
    ttk::labelframe $wc.n -text [mc "NAT Address"] \
      -padding [option get . groupSmallPadding {}]
    pack $wc.n -side top -fill x -anchor $anchor -pady 8
    
    BuildNATFrame $wc.n.f
    pack $wc.n.f -side top -fill x -anchor $anchor

    return $wpage
}

proc ::Proxy::BuildFrame {w} {
    global  prefs
    variable tmpPrefs
    variable wnoproxy
    variable wprx

    set wprx $w.prxy
    
    set prefs(noproxy) [lsort -unique $prefs(noproxy)]
    
    foreach key {
	setNATip    NATip 
	useproxy    proxy_type  proxy_host  proxy_port 
	proxy_user  proxy_pass  noproxy
    } {
	set tmpPrefs($key) $prefs($key)
    }
        
    variable wprxuse $wprx.use
    variable wuser   $wprx.euser
    variable wpass   $wprx.epass
    
    set menulist {
	{"HTTP Proxy"  -value http}
	{"SOCKS 4"     -value socks4}
	{"SOCKS 5"     -value socks5}
    }
        
    # Proxy.
    ttk::frame $w
    ttk::frame $wprx
    pack $wprx -side top -anchor [option get . dialogAnchor {}]
    
    ttk::label $wprx.msg -wraplength 300 -justify left \
      -text [mc "Usage of this HTTP proxy settings is determined by each profile's settings. File transfers will not work if you use an HTTP proxy."]
    ttk::checkbutton $wprx.use -text [mc "Use proxy"]  \
      -command [namespace code SetUseProxyState]  \
      -variable [namespace current]::tmpPrefs(useproxy)  
    ttk::label $wprx.lmb -text [mc "Type"]:
    ui::optionmenu $wprx.mb -menulist $menulist -direction flush \
      -command [namespace code SetProxyType] \
      -variable [namespace current]::tmpPrefs(proxy_type)
    ttk::label $wprx.lserv -text [mc "Host"]:
    ttk::entry $wprx.eserv -textvariable [namespace current]::tmpPrefs(proxy_host)
    ttk::label $wprx.lport -text [mc "Port"]:
    ttk::entry $wprx.eport -textvariable [namespace current]::tmpPrefs(proxy_port) \
      -width 6
    ttk::label $wprx.luser -text [mc "Username"]:
    ttk::entry $wprx.euser -textvariable [namespace current]::tmpPrefs(proxy_user)
    ttk::label $wprx.lpass -text [mc "Password"]:
    ttk::entry $wprx.epass -textvariable [namespace current]::tmpPrefs(proxy_pass) \
      -show {*}
  
    set wnoproxy $wprx.noproxy
    ttk::label $wprx.lnop -text [mc "Exclude addresses (* as wildcard)."]:
    text $wprx.noproxy -font CociSmallFont -height 4 -width 24 -bd 1 -relief sunken
    
    grid  $wprx.msg  -      -            -sticky w
    grid  $wprx.use  -      -            -sticky w
    grid  x  $wprx.lmb      $wprx.mb     -pady 1
    grid  x  $wprx.lserv    $wprx.eserv  -pady 1
    grid  x  $wprx.lport    $wprx.eport  -pady 1
    grid  x  $wprx.luser    $wprx.euser  -pady 1
    grid  x  $wprx.lpass    $wprx.epass  -pady 1
    grid  x  $wprx.lnop     -            -sticky w
    grid  x  $wprx.noproxy  -            -sticky ew -pady 1
    
    grid  $wprx.lmb  $wprx.lserv  $wprx.lport  $wprx.luser  $wprx.lpass  -sticky e
    grid  $wprx.mb   $wprx.eserv               $wprx.euser  $wprx.epass  -sticky ew
    grid  $wprx.eport  -sticky w
    
    grid columnconfigure $wprx 0 -minsize 12
    
    foreach addr $tmpPrefs(noproxy) {
	$wnoproxy insert end $addr
	$wnoproxy insert end "\n"
    }
    SetUseProxyState
    return $w
}

proc ::Proxy::BuildNATFrame {w} {
    global  prefs
    variable tmpPrefs
    variable wnat
    
    set wnat $w.nat
    variable wnatuse $wnat.use

    set stun 0
    if {![catch {package require stun}]} {
	set stun 1
    }

    foreach key {
	setNATip    NATip 
    } {
	set tmpPrefs($key) $prefs($key)
    }

    # NAT address.
    ttk::frame $w
    ttk::frame $wnat
    pack $wnat -side top -anchor [option get . dialogAnchor {}]

    ttk::checkbutton $wnat.use -text [mc "Use the following external address"] \
      -command [namespace code SetUseNATState]  \
      -variable [namespace current]::tmpPrefs(setNATip)
    ttk::entry $wnat.eip \
      -textvariable [namespace current]::tmpPrefs(NATip)
    if {$stun} {
	ttk::button $wnat.stun -text [mc "Detect"] -command ::Proxy::GetStun
    }
    
    grid  $wnat.use  -  -sticky w
    grid  $wnat.eip  x  -sticky ew
    grid columnconfigure $wnat 0 -weight 1
    if {$stun} {
	grid  $wnat.stun  -column 1 -row 1 -padx 4
    }
    SetUseNATState    
    return $w
}

proc ::Proxy::GetStun {} {
    ::stun::request stun01.sipphone.com -command ::Proxy::GetStunCB
}

proc ::Proxy::GetStunCB {token status args} {
    variable tmpPrefs
    
    array set argsA $args
    if {$status eq "ok" && [info exists argsA(-address)]} {
	set tmpPrefs(NATip) $argsA(-address)
    }   
}

proc ::Proxy::SetProxyType {type} {
    variable wuser
    variable wpass
    
    switch -- $type {
	socks5 {
	    $wuser state {!disabled}
	    $wpass state {!disabled}
	} 
	default {
	    $wuser state {disabled}
	    $wpass state {disabled}
	}
    }
}

proc ::Proxy::SetUseProxyState { } {
    variable wprx
    variable wprxuse
    variable tmpPrefs
    
    if {$tmpPrefs(useproxy)} {
	SetChildrenStates $wprx normal
    } else {
	SetChildrenStates $wprx disabled
    }
    $wprxuse state {!disabled}
}

proc ::Proxy::SetUseNATState { } {
    variable wnat
    variable wnatuse
    variable tmpPrefs
    
    if {$tmpPrefs(setNATip)} {
	SetChildrenStates $wnat normal
    } else {
	SetChildrenStates $wnat disabled
    }
    $wnatuse state {!disabled}
}

proc ::Proxy::SetChildrenStates {win _state} {
    variable tmpPrefs
    
    if {$_state eq "normal" || $_state eq "!disabled"} {
	set state normal
	set ttkstate {!disabled}
    } else {
	set state    disabled
	set ttkstate {disabled}
    }    
    foreach w [winfo children $win] {
	switch -- [winfo class $w] {
	    Text - Entry {
		$w configure -state $state
	    }
	    TEntry - TButton - TCheckbutton - TRadiobutton - TMenubutton {
		$w state $ttkstate
	    }
	}
    }
    if {$state eq "normal"} {
	SetProxyType $tmpPrefs(proxy_type)
    }
}

proc ::Proxy::GetNoProxyList { } {    
    variable wnoproxy

    set str [string trim [$wnoproxy get 1.0 end]]
    return [lsort -unique [split $str "\n"]]
}

proc ::Proxy::SavePrefsHook { } {
    global  prefs
    variable tmpPrefs
    variable wnoproxy
    
    set tmpPrefs(noproxy) [GetNoProxyList]
    array set prefs [array get tmpPrefs]
    unset tmpPrefs
    
    set prefs(proxy_user) [string trim $prefs(proxy_user)]
    set prefs(proxy_pass) [string trim $prefs(proxy_pass)]
    
    if {$prefs(useproxy)} {
	if {$prefs(proxy_type) eq "http"} {
	    AutoProxyConfig
	    AutoSocksOff
	} elseif {[string match {socks[45]} $prefs(proxy_type)]} {
	    AutoProxyOff
	    AutoSocksConfig
	}
    } else {
	AutoProxyOff
	AutoSocksOff
    }
}

proc ::Proxy::CancelPrefsHook { } {
    global  prefs
    variable tmpPrefs
    variable wnoproxy
    variable keySpecs
    
    set tmpPrefs(noproxy) [GetNoProxyList]
    	
    foreach key [array names keySpecs] {
	if {![string equal $prefs($key) $tmpPrefs($key)]} {
	    ::Preferences::HasChanged
	    break
	}
    }
    if {![string equal $prefs(noproxy) $tmpPrefs(noproxy)]} {
	::Preferences::HasChanged
    }
}

proc ::Proxy::UserDefaultsHook { } {
    global  prefs
    variable tmpPrefs
    variable wnoproxy
    variable keySpecs
    
    foreach key [array names keySpecs] {
	set tmpPrefs($key) $prefs($key)
    }
}

#-------------------------------------------------------------------------------