Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Windows support #286

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
29 changes: 29 additions & 0 deletions .github/workflows/create_bins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,32 @@ jobs:
with:
name: kr-${{ matrix.os }}.tar.gz
path: kr-${{ matrix.os }}.tar.gz


buildwin:
name: Build Win
runs-on: windows-latest

steps:
- name: Set up Go 1.13
uses: actions/setup-go@v1
with:
go-version: 1.13
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: build
run: build.bat
shell: cmd

- name: zip
run: Compress-Archive -Path bin\* -DestinationPath kr-windows-latest.zip
shell: powershell

- name: upload
uses: actions/upload-artifact@master
with:
name: kr-windows-latest.zip
path: kr-windows-latest.zip
6 changes: 6 additions & 0 deletions build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mkdir bin
cd src
go.exe build -v -trimpath -o ../bin/kr.exe ./kr
go.exe build -v -trimpath -o ../bin/krd.exe ./krd
go.exe build -v -trimpath -o ../bin/krssh.exe ./krssh
cd ..
27 changes: 27 additions & 0 deletions src/common/analytics/analytics_ua_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package analytics

import (
"fmt"
. "krypt.co/kr/common/version"
"sync"
)

// TODO
var analytics_user_agent = fmt.Sprintf("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Version/%s kr/%s", CURRENT_VERSION, CURRENT_VERSION)

const analytics_os = "Windows"

var cachedAnalyticsOSVersion *string
var osVersionMutex sync.Mutex

func getAnalyticsOSVersion() *string {
osVersionMutex.Lock()
defer osVersionMutex.Unlock()
if cachedAnalyticsOSVersion != nil {
return cachedAnalyticsOSVersion
}

//TODO: find system way to get version
// for now just use a constant here
return "WindowsOS"
}
2 changes: 2 additions & 0 deletions src/common/log/logging.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !windows

package log

import (
Expand Down
77 changes: 77 additions & 0 deletions src/common/log/logging_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package log

import (
"os"

"github.com/op/go-logging"

. "krypt.co/kr/common/socket"
)

var Log = logging.MustGetLogger("")
var syslogFormat = logging.MustStringFormatter(
`%{time:15:04:05.000} %{level:.6s} ▶ %{message}`,
)
var stderrFormat = logging.MustStringFormatter(
`%{color}Krypton ▶ %{message}%{color:reset}`,
)

func SetupLogging(prefix string, defaultLogLevel logging.Level, trySyslog bool) *logging.Logger {
var backend logging.Backend
/*
if trySyslog {
var err error
backend, err = logging.NewSyslogBackendPriority(prefix, syslog.LOG_NOTICE)
if err == nil {
logging.SetFormatter(syslogFormat)
// direct panic output to syslog as well
if syslogBackend, ok := backend.(*logging.SyslogBackend); ok {
stdlog.SetOutput(syslogBackend.Writer)
}
} else {
backend = nil
}

}
*/
if backend == nil {
var err error
var file *os.File
logName := prefix
if logName == "" {
logName = "kr"
}
logName += ".log"
path, err := KrDirFile(logName)
if err != nil {
file = os.Stderr
} else {
file, err = os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
file = os.Stderr
}
}
backend = logging.NewLogBackend(file, prefix, 0)
backend = logging.NewBackendFormatter(backend, stderrFormat)
}
leveled := logging.AddModuleLevel(backend)
switch os.Getenv("KR_LOG_LEVEL") {
case "CRITICAL":
leveled.SetLevel(logging.CRITICAL, prefix)
case "ERROR":
leveled.SetLevel(logging.ERROR, prefix)
case "WARNING":
leveled.SetLevel(logging.WARNING, prefix)
case "NOTICE":
leveled.SetLevel(logging.NOTICE, prefix)
case "INFO":
leveled.SetLevel(logging.INFO, prefix)
case "DEBUG":
leveled.SetLevel(logging.DEBUG, prefix)
default:
leveled.SetLevel(defaultLogLevel, prefix)
}

logging.SetBackend(leveled)
return Log
}
7 changes: 1 addition & 6 deletions src/common/socket/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func KrDirFile(file string) (fullPath string, err error) {

const AGENT_SOCKET_FILENAME = "krd-agent.sock"

func AgentListen() (listener net.Listener, err error) {
func AgentListenUnix() (listener net.Listener, err error) {
socketPath, err := KrDirFile(AGENT_SOCKET_FILENAME)
if err != nil {
return
Expand Down Expand Up @@ -177,8 +177,3 @@ func DaemonSocketOrFatal() (unixFile string) {
}
return
}

func IsKrdRunning() bool {
err := exec.Command("pgrep", "-U", User(), "krd").Run()
return nil == err
}
10 changes: 10 additions & 0 deletions src/common/socket/socket_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package socket
import (
"fmt"
"net"
"os/exec"
)

func DaemonDial(unixFile string) (conn net.Conn, err error) {
Expand All @@ -12,3 +13,12 @@ func DaemonDial(unixFile string) (conn net.Conn, err error) {
}
return
}

func IsKrdRunning() bool {
err := exec.Command("pgrep", "-U", User(), "krd").Run()
return nil == err
}

func AgentListen() (listener net.Listener, err error) {
return AgentListenUnix()
}
11 changes: 10 additions & 1 deletion src/common/socket/socket_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !darwin
// +build !darwin,!windows

package socket

Expand Down Expand Up @@ -37,3 +37,12 @@ func KillKrd() {
exec.Command("pkill", "-U", User(), "-x", "krd").Run()
<-time.After(1*time.Second)
}

func IsKrdRunning() bool {
err := exec.Command("pgrep", "-U", User(), "krd").Run()
return nil == err
}

func AgentListen() (listener net.Listener, err error) {
return AgentListenUnix()
}
72 changes: 72 additions & 0 deletions src/common/socket/socket_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// +build windows

package socket

import (
"bytes"
"fmt"
"github.com/Microsoft/go-winio"
"krypt.co/kr/common/util"
"net"
"os"
"os/exec"
"path/filepath"
"time"
)

const AGENT_PIPE = "\\\\.\\pipe\\krd-agent"

func AgentListen() (listener net.Listener, err error) {
listener, err = winio.ListenPipe(AGENT_PIPE, nil)
return
}

// TODO too much repeating...
func getPrefix() (string, error) {
if ex, err := os.Executable(); err == nil {
return filepath.Dir(ex), nil
} else {
return "", err
}
}

func DaemonDial(unixFile string) (conn net.Conn, err error) {
if !IsKrdRunning() {
os.Stderr.WriteString(util.Yellow("Krypton ▶ Restarting krd...\r\n"))
exe := "krd.exe"
if pfx, err := getPrefix(); err == nil {
exe = pfx + `\krd.exe`
}
_ = exec.Command(exe).Start()
<-time.After(1 * time.Second)
}
conn, err = net.Dial("unix", unixFile)
/*
TODO
if err != nil {
// restart then try again
os.Stderr.WriteString(Yellow("Krypton ▶ Restarting krd...\r\n"))
KillKrd()
exec.Command("nohup", "krd").Start()
<-time.After(1 * time.Second)
conn, err = net.Dial("unix", unixFile)
}
*/
if err != nil {
err = fmt.Errorf("Failed to connect to Krypton daemon. Please make sure it is running by typing \"kr restart\".")
}
return
}

func KillKrd() {
_ = exec.Command("taskkill", "/F", "/FI", `USERNAME eq ` + User(), "/IM", "krd.exe").Run()
<-time.After(1*time.Second)
}

func IsKrdRunning() bool {
cmd := exec.Command("tasklist", "/FI", `USERNAME eq ` + User(), "/FI", `IMAGENAME eq krd.exe`)
if ret, err := cmd.CombinedOutput(); err == nil {
return bytes.Contains(ret, []byte("krd.exe"))
}
return false
}
Loading