5
5
"errors"
6
6
"fmt"
7
7
"io"
8
+ "net"
8
9
"net/url"
9
10
"os/exec"
10
11
"strconv"
@@ -163,7 +164,7 @@ func handleHTTP(u string, showInfo bool) {
163
164
}
164
165
165
166
// handleOther is used by handleURL.
166
- // It opens or proxies links other than Gemini and HTTP and displays Error modals.
167
+ // It opens links other than Gemini and HTTP and displays Error modals.
167
168
func handleOther (u string ) {
168
169
// The URL should have a scheme due to a previous call to normalizeURL
169
170
parsed , _ := url .Parse (u )
@@ -331,15 +332,38 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) {
331
332
return ret ("" , false )
332
333
}
333
334
335
+ proxy := strings .TrimSpace (viper .GetString ("proxies." + parsed .Scheme ))
336
+ usingProxy := false
337
+
338
+ proxyHostname , proxyPort , err := net .SplitHostPort (proxy )
339
+ if err != nil {
340
+ // Error likely means there's no port in the host
341
+ proxyHostname = proxy
342
+ proxyPort = "1965"
343
+ }
344
+
334
345
if strings .HasPrefix (u , "http" ) {
335
- handleHTTP (u , true )
336
- return ret ("" , false )
346
+ if proxy == "" || proxy == "off" {
347
+ // No proxy available
348
+ handleHTTP (u , true )
349
+ return ret ("" , false )
350
+ } else {
351
+ usingProxy = true
352
+ }
337
353
}
338
- if ! strings .HasPrefix (u , "gemini" ) {
339
- handleOther (u )
340
- return ret ("" , false )
354
+
355
+ if ! strings .HasPrefix (u , "http" ) && ! strings .HasPrefix (u , "gemini" ) {
356
+ // Not a Gemini URL
357
+ if proxy == "" || proxy == "off" {
358
+ // No proxy available
359
+ handleOther (u )
360
+ return ret ("" , false )
361
+ } else {
362
+ usingProxy = true
363
+ }
341
364
}
342
- // Gemini URL
365
+
366
+ // Gemini URL, or one with a Gemini proxy available
343
367
344
368
// Load page from cache if possible
345
369
page , ok := cache .GetPage (u )
@@ -353,28 +377,33 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) {
353
377
t .mode = tabModeLoading
354
378
App .Draw ()
355
379
356
- res , err := client .Fetch (u )
380
+ var res * gemini.Response
381
+ if usingProxy {
382
+ res , err = client .FetchWithProxy (proxyHostname , proxyPort , u )
383
+ } else {
384
+ res , err = client .Fetch (u )
385
+ }
357
386
358
387
// Loading may have taken a while, make sure tab is still valid
359
388
if ! isValidTab (t ) {
360
389
return ret ("" , false )
361
390
}
362
391
363
392
if errors .Is (err , client .ErrTofu ) {
364
- if config .GemProxy == nil {
365
- if Tofu (parsed .Host , client .GetExpiry (parsed .Hostname (), parsed .Port ())) {
393
+ if usingProxy {
394
+ // They are using a proxy
395
+ if Tofu (proxy , client .GetExpiry (proxyHostname , proxyPort )) {
366
396
// They want to continue anyway
367
- client .ResetTofuEntry (parsed . Hostname (), parsed . Port () , res .Cert )
397
+ client .ResetTofuEntry (proxyHostname , proxyPort , res .Cert )
368
398
// Response can be used further down, no need to reload
369
399
} else {
370
400
// They don't want to continue
371
401
return ret ("" , false )
372
402
}
373
403
} else {
374
- // They are using a proxy
375
- if Tofu (config .GemProxy .Host , client .GetExpiry (config .GemProxy .Hostname (), config .GemProxy .Port ())) {
404
+ if Tofu (parsed .Host , client .GetExpiry (parsed .Hostname (), parsed .Port ())) {
376
405
// They want to continue anyway
377
- client .ResetTofuEntry (config . GemProxy . Hostname (), config . GemProxy .Port (), res .Cert )
406
+ client .ResetTofuEntry (parsed . Hostname (), parsed .Port (), res .Cert )
378
407
// Response can be used further down, no need to reload
379
408
} else {
380
409
// They don't want to continue
0 commit comments