(use rfc.http)
(use rfc.base64)
(use gauche.process)
(define chosenCountry "US")
(define URL "http://w...content-available-to-author-only...e.net/api/iphone/")
(define (url-split url)
(if-let1 m (#/https?:\/\/([^\/]+)(\/.+)/ url)
(values (m 1) (m 2))))
(define (prompt s)
(display s)
(flush (current-output-port))
(read-line))
(define (main args)
(when (< 1 (length args))
(set! chosenCountry (list-ref 1)))
(print "[autovpn] getting server list")
(let*-values (((domain path)
(url-split URL))
((status header body)
(http-get domain path)))
(print "[autovpn] parsing response")
(print "[autovpn] looking for " chosenCountry)
(call-with-input-string body
(lambda(p)
(read-line p)
(generator-map
(lambda(line)
(and-let* ((splits (string-split line #\,))
((<= 15 (length splits)))
(country (list-ref splits 6))
(conf (base64-decode-string (list-ref splits 14))))
(when (string=? country chosenCountry)
(with-output-to-file "/tmp/openvpnconf" (cut print conf))
(let1 process
(run-process '("sudo" "openvpn" "/tmp/openvpnconf"))
(with-signal-handlers
((SIGTERM (process-send-signal process SIGTERM)))
(process-wait process)))
(let1 i (prompt "[autovpn] try another VPN? (y/n)")
(when (and (string? i)
(< 0 (string-length i))
(char=? (char-downcase (string-ref i 0)) #\n))
(exit 0))))))
(cut read-line p)
)))))