Skip to content

Commit

Permalink
使用sdk重写
Browse files Browse the repository at this point in the history
  • Loading branch information
lerte committed Apr 14, 2024
1 parent 458966f commit 5f72b6c
Show file tree
Hide file tree
Showing 34 changed files with 1,646 additions and 631 deletions.
135 changes: 46 additions & 89 deletions app.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package main

import (
"bytes"
"context"
"fmt"
"net/http"
"os"
"strings"
"runtime/debug"
"zroker/zrok"

"github.com/gofiber/fiber/v2/log"
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/sdk/golang/sdk"
"github.com/wailsapp/wails/v2/pkg/runtime"
"mvdan.cc/xurls/v2"
)

// App struct
Expand Down Expand Up @@ -53,106 +51,65 @@ func (a *App) ChooseFolder() (string, error) {
return folder,nil
}

func writeToFile(name string, data string) error {
f, err := os.OpenFile(name, os.O_CREATE | os.O_APPEND | os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(data+"\n")
if err != nil {
return err
}
return nil
}

func (a *App) Invite(email string) string {
status := []string{"status"}
output, _ := zrokCmd(status).Output()

xurlsStrict := xurls.Strict()
find := xurlsStrict.FindAllString(string(output), -1)
apiEndpoint := find[0]

requestBody := []byte(fmt.Sprintf(`{"email": "%s"}`, email))
resp, err := http.Post(apiEndpoint+"/api/v1/invite", "application/zrok.v1+json", bytes.NewBuffer(requestBody))
if err != nil {
log.Error("发送请求时出错:", err)
}
defer resp.Body.Close()
return resp.Status
return zrok.Invite(email)
}

func (a *App) getEnvZId(status string) (string,string) {
arr := strings.Split(status, " ")
return arr[len(arr)-4],arr[len(arr)-9]
func (a *App) IsEnable() bool {
root, _ := environment.LoadRoot()
return root.IsEnabled()
}

func (a *App) Enable(token string) bool {
return zrok.Enable(token)
}

func (a *App) UnShare(shrToken string) string {
status := []string{"status", "--secrets"}
output, _ := zrokCmd(status).Output()

envZId, XToken := a.getEnvZId(string(output))

xurlsStrict := xurls.Strict()
find := xurlsStrict.FindAllString(string(output), -1)
apiEndpoint := find[0]

requestBody := []byte(fmt.Sprintf(`{"envZId": "%s", "shrToken": "%s"}`, envZId,shrToken ))
log.Info("requestBody", string(requestBody))
req, err := http.NewRequest("DELETE", apiEndpoint+"/api/v1/unshare", bytes.NewBuffer(requestBody))
req.Header.Add("Content-Type", "application/zrok.v1+json")
req.Header.Add("X-Token", XToken)
func (a *App) Disable() bool {
_,err := environment.LoadRoot()
return err == nil
}

if err != nil {
log.Error("创建请求出错:", err)
}

resp, err := http.DefaultClient.Do(req)

if err != nil {
log.Error("发送请求时出错:", err)
}
defer resp.Body.Close()
return resp.Status
func (a *App) DeleteShare(share *sdk.Share) bool {
root,_ := environment.LoadRoot()
err := sdk.DeleteShare(root, share)
return err == nil
}

func (a *App) Version() string {
version := []string{"version"}
output, _ := zrokCmd(version).Output()
return string(output)
bi,ok := debug.ReadBuildInfo()
if ok {
for _, dep := range bi.Deps {
if(dep.Path == "github.com/openziti/zrok") {
return dep.Version
}
}
}
return "unknown"
}

func (a *App) Overview() string {
overview := []string{"overview"}
output, _ := zrokCmd(overview).Output()
return string(output)
}

// Zrok
func (a *App) Zrok(args []string) string {
log.Info("Zrok", args)
writeToFile("./resources/logs.txt", strings.Join(args, " "))

cmd := zrokCmd(args)
// 创建一个缓冲区来保存标准错误输出
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
// 执行命令
err := cmd.Run()
root, err := environment.LoadRoot()
if err != nil {
// 如果命令执行出错,则打印标准错误输出
log.Error("执行命令时出错:", err)
log.Error("标准错误输出:", stderr.String())
return stderr.String()
return "error"
}
// 如果命令执行成功,则打印标准输出
overview,_ := sdk.Overview(root)
return overview
}

log.Info("标准输出:", stdout.String())
return stdout.String()
func (a *App) Sharing(shareRequest sdk.ShareRequest) *sdk.Share {
root,_ := environment.LoadRoot()
share,err := sdk.CreateShare(root, &sdk.ShareRequest{
BackendMode: shareRequest.BackendMode,
ShareMode: shareRequest.ShareMode,
Frontends: []string{"public"}, // 不是很懂这个参数
Target: shareRequest.Target,
})
if(err != nil){
log.Error("error", err)
}
log.Info("share", share)
return share
}

// Open link in browser
Expand Down
4 changes: 0 additions & 4 deletions build/windows/installer/project.nsi
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ Section
!insertmacro wails.writeUninstaller
SectionEnd

Section "Zrok"
SetOutPath $INSTDIR\resources
File /a ".\..\..\..\resources\zrok.exe"
SectionEnd

Section "uninstall"
!insertmacro wails.setShellContext
Expand Down
3 changes: 1 addition & 2 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
"lucide-react": "^0.367.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hot-toast": "^2.4.1",
"react-router-dom": "^6.22.3"
"react-hot-toast": "^2.4.1"
},
"devDependencies": {
"@types/react": "^18.2.75",
Expand Down
8 changes: 5 additions & 3 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import router from "./router";
import Zroker from "./Zroker";
import { isDark } from "./utils";
import { Theme } from "@radix-ui/themes";
import { Toaster } from "react-hot-toast";
import { useEffect, useState } from "react";
import { RouterProvider } from "react-router-dom";
import { EnableProvider } from "./contexts/Enable";
import { useAutoDark } from "./hooks/useAutoDark";

const App = () => {
Expand All @@ -22,7 +22,9 @@ const App = () => {
appearance={theme}
accentColor="crimson"
>
<RouterProvider router={router} />
<EnableProvider>
<Zroker />
</EnableProvider>
<Toaster
position="bottom-right"
toastOptions={{
Expand Down
17 changes: 0 additions & 17 deletions frontend/src/Layout.tsx

This file was deleted.

18 changes: 18 additions & 0 deletions frontend/src/Zroker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Flex } from "@radix-ui/themes";
import Login from "./components/Login";
import Overview from "./components/Overview";
import { useEnable } from "./contexts/Enable";

const Zroker = () => {
const { enable } = useEnable();

return (
<Flex
gap="4"
className="h-dvh p-4"
>
{enable ? <Overview /> : <Login />}
</Flex>
);
};
export default Zroker;
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import { useState } from "react";
import { Info } from "lucide-react";
import toast from "react-hot-toast";
import { Zrok, OpenExternal } from "../../wailsjs/go/main/App";
import { useEnable } from "../contexts/Enable";
import { Enable } from "../../wailsjs/go/main/App";
import { OpenExternal } from "../../wailsjs/go/main/App";
import { Card, Tooltip, Flex, Text, TextField, Button } from "@radix-ui/themes";

const Page = () => {
const EnablePage = () => {
const { setEnable } = useEnable();
const [command, setCommand] = useState("");
const [loading, setLoading] = useState(false);

const handleEnable = async () => {
setLoading(true);
const commands = command.split(" ").filter((item) => item != "");
commands.shift();
const result = await Zrok(commands);
if (/^\[ERROR\]/.test(result)) {
toast.error(result);
}
if (/successfully/.test(result)) {
toast.success("the zrok environment was successfully enabled...");
const token = commands.pop();
if (!token) {
toast.error("Invalid command");
} else {
const ok = await Enable(token);
if (ok) {
toast.success("Enable success");
setEnable(true);
} else {
toast.error("Enable failed");
}
}
setLoading(false);
};
Expand Down Expand Up @@ -52,9 +59,9 @@ const Page = () => {
<Button
size="3"
color="blue"
className="!block !w-full"
loading={loading}
onClick={handleEnable}
className="!block !w-full"
>
Enable
</Button>
Expand All @@ -63,4 +70,4 @@ const Page = () => {
);
};

export default Page;
export default EnablePage;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from "react";
import toast from "react-hot-toast";
import { Card, Flex, Text, TextField, Button } from "@radix-ui/themes";
import { Invite } from "../../wailsjs/go/main/App";
import { Card, Flex, Text, TextField, Button } from "@radix-ui/themes";

const Page = () => {
const [email, setEmail] = useState("");
Expand Down
38 changes: 38 additions & 0 deletions frontend/src/components/Login.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Invite from "./Invite";
import Enable from "./Enable";
import { Box, Card, Flex, Tabs } from "@radix-ui/themes";

const Login = () => {
return (
<Card className="w-full">
<Flex
gap="6"
align="center"
justify="center"
direction="column"
className="h-full w-full mx-auto"
>
<Tabs.Root
defaultValue="invite"
className="w-[480px] md:w-[600px]"
>
<Tabs.List>
<Tabs.Trigger value="invite">Invite</Tabs.Trigger>
<Tabs.Trigger value="enable">Enable</Tabs.Trigger>
</Tabs.List>
<Box pt="3">
<Tabs.Content value="invite">
<Invite />
</Tabs.Content>

<Tabs.Content value="enable">
<Enable />
</Tabs.Content>
</Box>
</Tabs.Root>
</Flex>
</Card>
);
};

export default Login;
Loading

0 comments on commit 5f72b6c

Please sign in to comment.