Commit c3386141 authored by Nong Hoang Tu's avatar Nong Hoang Tu
Browse files

Remove old modules and rework make_torrc

parent 60f14d21
Pipeline #3774 canceled with stages
type
callback_kill_apps* = proc(callback_send_msg: callback_send_messenger) {.closure.}
callback_send_messenger* = proc(title, body: string, code: int)
\ No newline at end of file
callback_send_messenger* = proc(title, body: string, code: int)
TorConfig* = object
fileErr*: bool # Error while reading file
controlPort*: string
transPort*: string
socksPort*: string
dnsPort*: string
AnonOptions* = object
use_bridge*: bool
custom_bridge*: bool
bridge_addr*: string
let
bridgePath* = "/etc/anonsurf/bridges.txt"
defConfPath* = "/etc/anonsurf/anonrc"
import os
import strutils
type
TorConfig* = object
fileErr*: bool # Error while reading file
controlPort*: string
transPort*: string
socksPort*: string
dnsPort*: string
import .. / ansurf_types
import json
proc getTorrcPorts*(): TorConfig =
......@@ -65,3 +59,28 @@ proc toNixHex*(conf: TorConfig): TorConfig =
result.transPort = handleParse(conf.transPort)
except:
result.fileErr = true
proc readConf*(p: string): AnonOptions =
let defConfVal = AnonOptions(
use_bridge: false,
custom_bridge: false,
bridge_addr: "",
)
if fileExists(p):
var jnode = parseFile(p)
return to(jnode, AnonOptions)
else:
stderr.write("[x] Config doesn't exists. Use default options\n")
return defConfVal
proc readDefaultConfig*(): AnonOptions =
return readConf(defConfPath)
proc writeConf*(p: string, op: AnonOptions) =
try:
writeFile(p, pretty(%op))
except:
stderr.write("[x] Error while writing config at " & p & "\n")
import random
import strutils
import osproc
import .. / cores / options
import commons / parse_config
randomize()
......@@ -78,7 +78,7 @@ proc genTorrc*(hashed: string): string =
result &= "# Customize your tor configuration by editing /etc/anonsurf/torrc.base\n"
result &= readFile(basePath)
result &= "\nHashedControlPassword " & hashed & "\n"
let conf = readDefaultConfig() # TODO user's config at home
let conf = readDefaultConfig()
if conf.use_bridge == false:
result &= "# Enable sandbox\nSandbox 1\n"
else:
......
import gintro / [gtk, glib]
proc onClickBoot*(b: Button) =
#[
Create condition to enable or disable anonsurf at boot
when we click on button
]#
if b.label == "Enable":
if spawnCommandLineAsync("gksudo /usr/bin/systemctl enable anonsurfd"):
b.label = "Enabling"
else:
discard
else:
if spawnCommandLineAsync("gksudo /usr/bin/systemctl disable anonsurfd"):
b.label = "Disabling"
else:
discard
import gintro / [gtk, glib, vte, gobject]
# import .. / modules / [myip, changeid, runsurf]
# import .. / displays / [noti, askKill]
# import strutils
import system
import os
# import .. / cores /
type
MyIP = object
thisAddr*: string
isUnderTor*: string
iconName*: string
var
worker*: system.Thread[void]
channel*: Channel[MyIP]
channel.open()
proc doCheckIP() =
#[
Actual IP check for AnonSurf
It parses data from server and show notification
]#
var finalResult: MyIP
let ipInfo = checkIPFromTorServer()
# If program has error while getting IP address
if ipInfo[0].startsWith("Error"):
finalResult.iconName = "error"
# If program runs but user didn't connect to tor
elif ipInfo[0].startsWith("Sorry"):
finalResult.iconName = "security-medium"
# Connected to tor
else:
finalResult.iconName = "security-high"
finalResult.isUnderTor = ipInfo[0]
finalResult.thisAddr = ipInfo[1]
channel.send(finalResult)
proc onClickCheckIP*(b: Button) =
#[
Display IP when user click on CheckIP button
Show the information in system's notification
]#
sendNotify("My IP", "Getting data from server", "dialog-information")
createThread(worker, doCheckIP)
proc onClickRun*(b: Button) =
#[
Create condition to Start / Stop AnonSurf when click on btn1
]#
if b.label == "Start":
initAskDialog()
createThread(worker, start)
else:
retCode.open()
createThread(worker, stop)
worker.joinThread()
#[
Check return code here before ask
1. If user typed password (and it is correct):
return code == 0 -> ask
2. if user cancelled gksudo:
return code == 255. Do not ask
3. error or wrong password ?
return code == 3 for wrong password (3 times)
]#
let stopResult = retCode.tryRecv()
if stopResult.dataAvailable and stopResult.msg == 0:
initAskDialog()
retcode.close()
proc onClickRestart*(b: Button) =
#[
Run anonsurf restart
]#
# if spawnCommandLineAsync("gksudo /usr/bin/anonsurf restart"):
# let imgStatus = newImageFromIconName("system-restart-panel", 3)
# b.setImage(imgStatus)
# else:
# discard
# discard spawnCommandLineAsync("gksudo /usr/sbin/service anonsurfd restart")
createThread(worker, restart)
proc onClickChangeID*(b: Button) =
#[
Use control port to change ID of Tor network
1. Read password from nyxrc
2. Get ControlPort from Torrc
3. Send authentication request + NewNYM command
]#
let conf = "/etc/anonsurf/nyxrc"
if fileExists(conf):
try:
let recvData = doChangeID(conf)
if recvData[0] != "250 OK\c":
sendNotify("Identity Change Error", recvData[0], "error")
else:
if recvData[1] != "250 OK\c":
sendNotify("Identity Change Error", recvData[1], "error")
else:
# Success. Show okay
sendNotify("Identity Change Success", "You have a new identity", "security-high")
# else:
# sendNotify("Identity Change Error", "Can parse settings", "error")
except:
sendNotify("Identity Change Error", "Error while connecting to control port", "security-low")
echo getCurrentExceptionMsg()
else:
sendNotify("Identity Change Error", "File not found", "error")
proc upgradeCallback(terminal: ptr Terminal00; pid: int32; error: ptr glib.Error; userData: pointer) {.cdecl.} =
#[
Dummy callback proc to fix problem of VTE
]#
discard
proc onVTEExit(v: Terminal, signal: int, d: Dialog) =
d.destroy()
proc onClickTorStatus*(b: Button) =
#[
Spawn a native GTK terminal and run nyx with it to show current tor status
]#
let
statusDialog = newDialog()
statusArea = statusDialog.getContentArea()
nyxTerm = newTerminal()
statusDialog.setTitle("Tor bandwidth")
nyxTerm.connect("child-exited", onVTEExit, statusDialog)
nyxTerm.spawnAsync(
{noLastlog}, # pty flags
nil, # working directory
["/usr/bin/nyx", "--config", "/etc/anonsurf/nyxrc"], # args
[], # envv
{}, # spawn flag
nil, # Child setup
nil, # child setup data
nil, # chlid setup data destroy
-1, # timeout
nil, # cancellabel
upgradeCallback, # callback
nil, # pointer
)
statusArea.packStart(nyxTerm, false, true, 3)
statusDialog.showAll()
import gintro / gtk
import actMainPage
proc onClickDetail*(b: Button, s: Stack) =
#[
Display details widget or main widget
]#
if b.label == "Details":
s.setVisibleChildName("detail")
b.label = "Back"
else:
s.setVisibleChildName("main")
b.label = "Details"
proc onClickExit*(b: Button) =
#[
Close program by click on exit button
]#
channel.close()
mainQuit()
proc onClickStop*(w: Window) =
#[
Close program by click on title bar
]#
channel.close()
mainQuit()
import gintro / gdkpixbuf
#[
Load image from icons make to "embedded" icons
]#
type
AnonImgBuf* = object
imgSecLow*: Pixbuf
imgSecMed*: Pixbuf
imgSecHigh*: Pixbuf
imgBootOn*: Pixbuf
imgBootOff*: Pixbuf
proc initImgs(): AnonImgBuf =
const
imgSecLow = staticRead("../../../../icons/sec-low.svg")
imgSecMed = staticRead("../../../../icons/sec-med.svg")
imgSecHigh = staticRead("../../../../icons/sec-high.svg")
imgBootOff = staticRead("../../../../icons/boot-off.svg")
imgBootOn = staticRead("../../../../icons/boot-on.svg")
let loadLow = newPixbufLoader()
discard loadLow.write(imgSecLow)
discard loadLow.close()
result.imgSecLow = loadLow.getPixbuf()
let loadMed = newPixbufLoader()
discard loadMed.write(imgSecMed)
discard loadMed.close()
result.imgSecMed = loadMed.getPixbuf()
let loadHigh = newPixbufLoader()
discard loadHigh.write(imgSecHigh)
discard loadHigh.close()
result.imgSecHigh = loadHigh.getPixbuf()
let loadOff = newPixbufLoader()
discard loadOff.write(imgBootOff)
discard loadOff.close()
result.imgBootOff = loadOff.getPixbuf()
let loadOn = newPixbufLoader()
discard loadOn.write(imgBootOn)
discard loadOn.close()
result.imgBootOn = loadOn.getPixbuf()
proc surfIconPixbuf(): Pixbuf =
const
imgData = staticRead("../../../../icons/anonsurf.png")
let loadData = newPixbufLoader()
discard loadData.write(imgData)
discard loadData.close()
result = loadData.getPixbuf()
proc exitIconPixbuf(): Pixbuf =
const
imgData = staticRead("../../../../icons/exit.png")
let loadData = newPixbufLoader()
discard loadData.write(imgData)
discard loadData.close()
result = loadData.getPixbuf()
proc aboutIconPixbuf(): Pixbuf =
const
imgData = staticRead("../../../../icons/help-about.png")
let loadData = newPixbufLoader()
discard loadData.write(imgData)
discard loadData.close()
result = loadData.getPixbuf()
let
surfImages* = initImgs()
surfIcon* = surfIconPixbuf()
# reloadIcon* = reloadIconPixbuf()
exitIcon* = exitIconPixbuf()
aboutIcon* = aboutIconPixbuf()
#[
https://nim-lang.org/docs/json.html
str -> json: parseJson()
json -> str: $
obj -> json: %
json -> obj: to()
]#
import json
import os
type
AnonOptions* = object
use_bridge*: bool
custom_bridge*: bool
bridge_addr*: string
let
bridgePath* = "/etc/anonsurf/bridges.txt"
defConfPath* = "/etc/anonsurf/anonrc"
defConfVal = AnonOptions(
use_bridge: false,
custom_bridge: false,
bridge_addr: "",
)
proc readConf*(p: string): AnonOptions =
if fileExists(p):
var jnode = parseFile(p)
return to(jnode, AnonOptions)
else:
stderr.write("[x] Config doesn't exists. Use default options\n")
return defConfVal
proc readDefaultConfig*(): AnonOptions =
return readConf(defConfPath)
proc writeConf*(p: string, op: AnonOptions) =
try:
writeFile(p, pretty(%op))
except:
stderr.write("[x] Error while writing config at " & p & "\n")
import gintro / gtk
import .. / .. / utils / dnsutils
import status
import .. / actions / actMainPage
import images
import .. / displays / noti
import strutils
type
MainObjs* = ref object
btnRun*: Button
btnID*: Button
btnDetail*: Button
btnStatus*: Button
btnIP*: Button
lDetails*: Label
imgStatus*: Image
btnRestart*: Button
DetailObjs* = ref object
lblServices*: Label
lblPorts*: Label
lblDns*: Label
lblBoot*: Label
btnBoot*: Button
imgBoot*: Image
proc updateDetail*(args: DetailObjs, myStatus: Status) =
# AnonSurf is Enabled at boot
if myStatus.isAnonSurfBoot:
args.btnBoot.label = "Disable"
args.lblBoot.setLabel("Enabled at boot")
# args.imgBoot.setFromIconName("security-high", 6)
args.imgBoot.setFromPixbuf(surfImages.imgBootOn)
else:
args.btnBoot.label = "Enable"
args.lblBoot.setLabel("Not enabled at boot")
# args.imgBoot.setFromIconName("security-low", 6)
args.imgBoot.setFromPixbuf(surfImages.imgBootOff)
# Check current status of daemon services and control ports
if myStatus.isAnonSurfService == 1:
# Check status of Tor
if myStatus.isTorService == 1:
args.lblServices.setMarkup("Servc: <b><span background=\"#333333\" foreground=\"#00FF00\">Activated</span></b>")
elif myStatus.isTorService == 0:
# Give error msg with red color
args.lblServices.setMarkup("Servc: <b><span background=\"#333333\" foreground=\"#FF0000\">Tor is not running</span></b>")
elif myStatus.isTorService == -1:
# Give error msg with red color
args.lblServices.setMarkup("Servc: <b><span background=\"#333333\" foreground=\"#FF0000\">Can't start Tor</span></b>")
# Check status of Port
let myPorts = getStatusPorts()
if myPorts.isReadError:
# Give error msg with red color
args.lblPorts.setMarkup("Ports: <b><span background=\"#333333\" foreground=\"#FF0000\">Parse torrc failed</span></b>")
else:
var
onErrPorts: seq[string]
szErr = 0
if not myPorts.isControlPort:
onErrPorts.add("Control")
szErr += 1
if not myPorts.isSocksPort:
onErrPorts.add("Socks")
szErr += 1
if not myPorts.isTransPort:
onErrPorts.add("Trans")
szErr += 1
if szErr == 0:
# ACtivated green
args.lblPorts.setMarkup("Ports: <b><span background=\"#333333\" foreground=\"#00FF00\">Activated</span></b>")
elif szErr == 3:
# Give error msg with red color
args.lblPorts.setMarkup("Ports: <b><span background=\"#333333\" foreground=\"#FF0000\">Can't bind ports</span></b>")
elif szErr == 2:
# Give error msg with red color
args.lblPorts.setMarkup("Ports: <b><span background=\"#333333\" foreground=\"#FF0000\">Error on " & join(onErrPorts, ", ") & "</span></b>")
else:
# Give error msg with red color
args.lblPorts.setMarkup("Ports: <b<span background=\"#333333\" foreground=\"#FF0000\">>Error on " & onErrPorts[0] & "</span></b>")
elif myStatus.isAnonsurfSErvice == 0:
# Deactivated cyan color
args.lblServices.setMarkup("Servc: <b><span background=\"#333333\" foreground=\"#00FFFF\">Deactivated</span></b>")
args.lblPorts.setMarkup("Ports: <b><span background=\"#333333\" foreground=\"#00FFFF\">Deactivated</span></b>")
else:
# Deactivated cyan color and error red color
args.lblServices.setMarkup("Servc: <b><span background=\"#333333\" foreground=\"#FF0000\">Can't start AnonSurf</span></b>")
args.lblPorts.setMarkup("Ports: <b><span background=\"#333333\" foreground=\"#00FFFF\">Deactivated</span></b>")
# Update DNS status
# TODO remove all text shadow
case dnsStatusCheck()
of STT_DNS_TOR:
let myPorts = getStatusPorts()
if myPorts.isReadError:
# ERROR RED
args.lblDns.setMarkup("DNS: <b><span background=\"#333333\" foreground=\"#FF0000\"> Can't read Tor config<span></b>")
elif myPorts.isDNSPort:
# Activated green
args.lblDns.setMarkup("DNS: <b><span background=\"#333333\" foreground=\"#00FF00\">Activated</span></b>")
else:
# Give error msg with red color
args.lblDns.setMarkup("DNS: <b><span background=\"#333333\" foreground=\"#FF0000\"> Can't bind port</span></b>")
of ERROR_DNS_LOCALHOST:
# Give error style with red color
args.lblDns.setMarkup("DNS: <b><span background=\"#333333\" foreground=\"#FF0000\"> LocalHost</span></b>")
of ERROR_FILE_EMPTY:
# Give error msg with red color
args.lblDns.setMarkup("DNS: <b><span background=\"#333333\" foreground=\"#FF0000\">resolv.conf is empty</span></b>")
of ERROR_FILE_NOT_FOUND:
# Give error msg with red color
args.lblDns.setMarkup("DNS: <b><span background=\"#333333\" foreground=\"#FF0000\">resolv.conf not found</span></b>")
of ERROR_UNKNOWN:
# Give error msg with red color
args.lblDns.setMarkup("DNS: <b><span background=\"#333333\" foreground=\"#FF0000\">Unknown error</span></b>")
of 11:
# Use cyan for opennic or custom addresses
args.lblDns.setMarkup("DNS: <b><span background=\"#333333\" foreground=\"#00FFFF\">OpenNIC server</span></b>")
of 21:
# Use cyan for opennic or custom addresses
args.lblDns.setMarkup("DNS: <b><span background=\"#333333\" foreground=\"#00FFFF\">OpenNIC server</span></b>")
else:
# Use cyan for opennic or custom addresses
args.lblDns.setMarkup("DNS: <b><span background=\"#333333\" foreground=\"#00FFFF\">Custom setting</span></b>")
proc updateMain*(args: MainObjs, myStatus: Status) =
#[
Always check status of current widget
to show correct state of buttons
]#
# args.btnRestart.label = "Restart"
if myStatus.isAnonSurfService == 1:
# Idea: Restart when AnonSurf is failed
args.btnRestart.setSensitive(true)
# Check status of tor service
if myStatus.isTorService == 1:
let myPorts = getStatusPorts()
# If everything (except DNS port) is okay
if myPorts.isControlPort and myPorts.isSocksPort and myPorts.isTransPort and
not myPorts.isReadError:
args.btnID.setSensitive(true)
args.btnStatus.setSensitive(true)
# Check DNS
if myPorts.isDNSPort:
# args.imgStatus.setFromIconName("security-high", 6)
args.imgStatus.setFromPixBuf(surfImages.imgSecHigh)
args.lDetails.setText("AnonSurf is running")
else:
# args.imgStatus.setFromIconName("security-medium", 6)
args.imgStatus.setFromPixBuf(surfImages.imgSecMed)
args.lDetails.setText("Error with DNS port")
else:
# args.imgStatus.setFromIconName("security-low", 6)
args.imgStatus.setFromPixBuf(surfImages.imgSecLow)
args.lDetails.setText("Error with Tor ports")
args.btnID.setSensitive(false)
args.btnStatus.setSensitive(false)
else:
# args.imgStatus.setFromIconName("security-low", 6)
args.imgStatus.setFromPixBuf(surfImages.imgSecLow)
args.lDetails.setText("Tor service doesn't start")
args.btnID.setSensitive(false)
args.btnStatus.setSensitive(false)
args.btnRun.label = "Stop"
else:
if myStatus.isAnonSurfService == -1:
args.btnRestart.setSensitive(false)
args.lDetails.setText("AnonSurf start failed") # Fix me