Permalink
Browse files

config dir paths are procs now instead of constants

  • Loading branch information...
hypatia2 committed Mar 5, 2016
1 parent 5ad3958 commit edee4cb4c4e23bc27d21d69c84109c63bb7108d2
Showing with 56 additions and 37 deletions.
  1. +1 −1 fruho/add_from_file.tcl
  2. +15 −15 fruho/main.tcl
  3. +31 −21 fruho/model.tcl
  4. +9 −0 sklib/unix.tcl
@@ -68,7 +68,7 @@ proc ::from_file::add-to-treeview-plist {plist} {
proc ::from_file::SelectFileClicked {pconf} {
# "Select configuration files"
# "Press Control to select multiple files"
set files [tk_getOpenFile -multiple 1 -initialdir /home/sk/configbundles/sk1]
set files [tk_getOpenFile -multiple 1] ;# possible -initialdir option
if {$files ne ""} {
set ::model::Gui_selected_files $files
$pconf.select.msg configure -text "[llength $::model::Gui_selected_files] file(s) selected"
@@ -63,13 +63,13 @@ interp bgerror "" background-error
# We need to redirect to log file here and not in external shell script
# in case it is run with sudo. Then logging would go to /root/.fruho
# Redirect stdout to a file $::model::LOGFILE
# Redirect stdout to a file model LOGFILE
namespace eval tolog {
variable fh
proc initialize {args} {
variable fh
# if for some reason cannot log to file, log to stderr
if {[catch {mk-head-dir $::model::LOGFILE} out err] == 1 || [catch {set fh [open $::model::LOGFILE w]} out err] == 1} {
if {[catch {mk-head-dir [model LOGFILE]} out err] == 1 || [catch {set fh [open [model LOGFILE] w]} out err] == 1} {
set fh stderr
puts stderr $err
}
@@ -143,7 +143,7 @@ proc main {} {
# Copy cadir because it must be accessible from outside of the starkit
# Overwrites certs on every run
copy-merge [file join [file dir [info script]] certs] $::model::CADIR
copy-merge [file join [file dir [info script]] certs] [model CADIR]
if {$params(cli) || ![unix is-x-running] || $params(version) || $params(id) || $params(generate-keys) || $params(add-launcher) || $params(remove-launcher)} {
set ::model::Ui cli
@@ -190,7 +190,7 @@ proc main {} {
set ::model::Running_binary_fingerprint [sha1sum [this-binary]]
set ::model::OPENVPNLOG [open $::model::OPENVPNLOGFILE w]
set ::model::Openvpnlog [open [model OPENVPNLOGFILE] w]
in-ui main
daemon-monitor
go ffread-loop
@@ -209,7 +209,7 @@ proc main-exit {{arg ""}} {
delete-pidfile ~/.fruho/fruho.pid
set ::until_exit 1
catch {close [$::model::Ffconn_sock}
catch {close $::model::OPENVPNLOG}
catch {close $::model::Openvpnlog}
catch {destroy .}
exit
}
@@ -227,7 +227,7 @@ proc error-gui {msg} {
# hide toplevel window. Use wm deiconify to restore later
catch {wm withdraw .}
log $msg
tk_messageBox -title "fruho error" -type ok -icon error -message ERROR -detail "$msg\n\nPlease check $::model::LOGFILE for details"
tk_messageBox -title "fruho error" -type ok -icon error -message ERROR -detail "$msg\n\nPlease check [model LOGFILE] for details"
} else {
error-cli $msg
}
@@ -333,7 +333,7 @@ proc main-gui {} {
label .mainstatusline.spin
img place 16/empty .mainstatusline.spin
# TODO consider logging editor stderr/stdout to a file for debugging
hyperlink .mainstatusline.link -command [list exec xdg-open [file normalize $::model::OPENVPNLOGFILE] >>& /dev/null &]
hyperlink .mainstatusline.link -command [list exec xdg-open [file normalize [model OPENVPNLOGFILE]] >>& /dev/null &]
grid .mainstatusline.msg .mainstatusline.spin .mainstatusline.link -padx 5
grid .mainstatusline -sticky news -padx 10
@@ -855,7 +855,7 @@ proc rebuild-slist-with-geoloc {slist loc} {
proc ovpndir {profileid args} {
return [file join $::model::PROFILEDIR [name2id $profileid] ovpnconf default {*}$args]
return [file join [model PROFILEDIR] [name2id $profileid] ovpnconf default {*}$args]
}
@@ -884,7 +884,7 @@ proc is-config-received {profileid} {
proc curl-dispatch {chout cherr hostport args} {
if {[string match bootstrap:* $hostport]} {
curl-retry $chout $cherr -hostports $::model::Hostports -hindex ::model::hostport_lastok -expected_hostname vbox.fruho.com -cadir $::model::CADIR {*}$args
curl-retry $chout $cherr -hostports $::model::Hostports -hindex ::model::hostport_lastok -expected_hostname vbox.fruho.com -cadir [model CADIR] {*}$args
} else {
curl-retry $chout $cherr -hostports [lrepeat 3 $hostport] {*}$args
}
@@ -1970,7 +1970,7 @@ proc UpdateNowClicked {uframe} {
# tolerates uframe being empty string, GUI not updated then
proc download-get-update {{uframe ""}} {
try {
set dir [file join $::model::UPGRADEDIR $::model::Latest_version]
set dir [file join [model UPGRADEDIR] $::model::Latest_version]
file mkdir $dir
set zipfile $dir/update.zip
if {![file isfile $zipfile]} {
@@ -2761,15 +2761,15 @@ proc ffread-loop {} {
}
{^OpenVPN ERROR} {
$::model::Chan_openvpn_fail <- [lindex $tokens 1]
puts $::model::OPENVPNLOG [lindex $tokens 1]
flush $::model::OPENVPNLOG
puts $::model::Openvpnlog [lindex $tokens 1]
flush $::model::Openvpnlog
}
}
}
{^ovpn: (.*)$} {
catch {
puts $::model::OPENVPNLOG [lindex $tokens 1]
flush $::model::OPENVPNLOG
puts $::model::Openvpnlog [lindex $tokens 1]
flush $::model::Openvpnlog
}
switch -regexp -matchvar details [lindex $tokens 1] {
{^Initialization Sequence Completed} {
@@ -3015,7 +3015,7 @@ proc ClickConnect {} {
ffwrite "config $localconf"
# append newlines to openvpn log for better readability
puts $::model::OPENVPNLOG "\n\n"
puts $::model::Openvpnlog "\n\n"
$::model::Chan_button_connect <- 1
}
@@ -13,21 +13,6 @@ namespace eval ::model {
namespace export *
namespace ensemble create
########################################
# Constants
########################################
variable HOME [file normalize ~]
variable CONFIGDIR [file join $HOME .fruho]
variable INIFILE [file join $CONFIGDIR fruho.ini]
variable LOGFILE [file join $CONFIGDIR fruho.log]
variable OPENVPNLOGFILE [file join $CONFIGDIR openvpn.log]
variable OPENVPNLOG ""
variable PROFILEDIR [file join $CONFIGDIR profile]
variable UPGRADEDIR [file join $CONFIGDIR upgrade]
variable CADIR [file join $CONFIGDIR certs]
########################################
# General globals
########################################
@@ -139,6 +124,8 @@ namespace eval ::model {
# file descriptor for OPENVPNLOGFILE
variable Openvpnlog {}
# client id
variable Cn ""
@@ -156,6 +143,29 @@ namespace eval ::model {
}
# Moved from constants to procs since these might change when relinquish root
proc ::model::CONFIGDIR {} {
return [file join [unix homedir] .fruho]
}
proc ::model::INIFILE {} {
return [file join [model CONFIGDIR] fruho.ini]
}
proc ::model::LOGFILE {} {
return [file join [model CONFIGDIR] fruho.log]
}
proc ::model::OPENVPNLOGFILE {} {
return [file join [model CONFIGDIR] openvpn.log]
}
proc ::model::PROFILEDIR {} {
return [file join [model CONFIGDIR] profile]
}
proc ::model::UPGRADEDIR {} {
return [file join [model CONFIGDIR] upgrade]
}
proc ::model::CADIR {} {
return [file join [model CONFIGDIR] certs]
}
# Display all model variables to stderr
proc ::model::print {} {
puts stderr "MODEL:"
@@ -246,20 +256,20 @@ proc ::model::load-bootstrap {} {
# and populate to model ns
proc ::model::load {} {
if {[catch {
ini2model $::model::INIFILE
ini2model [model INIFILE]
::model::load-bootstrap
# load profiles from individual directories and update model
set profiles [lmap d [glob -directory $::model::PROFILEDIR -nocomplain -type d *] {file tail $d}]
set profiles [lmap d [glob -directory [model PROFILEDIR] -nocomplain -type d *] {file tail $d}]
# sanitize directory names
foreach p $profiles {
if {![regexp {^[\w\d_]+$} $p]} {
fatal "Profile directory name should be alphanumeric string in $::model::PROFILEDIR"
fatal "Profile directory name should be alphanumeric string in [model PROFILEDIR]"
}
}
foreach p $profiles {
set inifile [file join $::model::PROFILEDIR $p config.ini]
set inifile [file join [model PROFILEDIR] $p config.ini]
if {[file exists $inifile]} {
dict set ::model::Profiles $p [inicfg load $inifile]
}
@@ -277,10 +287,10 @@ proc ::model::load {} {
# may throw errors
proc ::model::save {} {
# save main ini
::model::model2ini $::model::INIFILE
::model::model2ini [model INIFILE]
# save profile inis
dict for {p d} $::model::Profiles {
set inifile [file join $::model::PROFILEDIR $p config.ini]
set inifile [file join [model PROFILEDIR] $p config.ini]
::model::dict2ini $d $inifile
}
}
@@ -98,3 +98,12 @@ proc ::unix::remove-launcher {appname} {
file delete $launcher
}
}
# Linux user home directory
# This is improved/resilient to sudo/su changes
# The problem was that when running the program with sudo
# and then relinquish root the username was changed but on Debian $HOME was pointing to /root
proc ::unix::homedir {} {
return [file normalize ~[id user]]
}

0 comments on commit edee4cb

Please sign in to comment.