tkstar

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2026 License: Apache-2.0 Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

tkstar

tkstar 是一个 Go 工具包集合,包含以下能力:

  • tkstar:通用辅助函数
  • authorization:远程授权校验
  • network:从 curl 字符串发起请求
  • text:RSA/AES 与文本处理
  • screen:Windows 屏幕悬浮文本
  • hardware:Windows 硬件和按键检测
  • edge:基于 chromedp 的 Edge 浏览器控制

安装

go get github.com/2Kil/tkstar

包说明

  • screen、hardware、edge 依赖 Windows API,仅适合 Windows 环境。
  • screen.ScreenInit() 会进入消息循环,必须放在 goroutine 中或主线程最后执行。
  • edge 包依赖本机安装 Microsoft Edge。
  • authorization 依赖远程二维码页面格式,示例中的地址和密码请替换为实际值。

tkstar 包

导入:

import "github.com/2Kil/tkstar"
func IsDebug() bool

判断当前是否接近 go run 的临时构建环境。

package main

import (
	"fmt"

	"github.com/2Kil/tkstar"
)

func main() {
	fmt.Println("debug:", tkstar.IsDebug())
}
func RandAtomic(max int) int

返回 [0, max) 的随机整数。

package main

import (
	"fmt"

	"github.com/2Kil/tkstar"
)

func main() {
	n := tkstar.RandAtomic(10)
	fmt.Println(n)
}
func RandAtomicRadius(min, max int) int

返回 [min, max] 的随机整数。

package main

import (
	"fmt"

	"github.com/2Kil/tkstar"
)

func main() {
	n := tkstar.RandAtomicRadius(5, 12)
	fmt.Println(n)
}
func CheckErr(err error, errString ...string) bool

统一打印错误并返回是否成功。

package main

import (
	"errors"
	"fmt"

	"github.com/2Kil/tkstar"
)

func main() {
	ok := tkstar.CheckErr(errors.New("open failed"), "load config")
	fmt.Println("ok:", ok)
}
func BuildTime() string

返回当前可执行文件最后修改时间。

package main

import (
	"fmt"

	"github.com/2Kil/tkstar"
)

func main() {
	fmt.Println("build time:", tkstar.BuildTime())
}
func HelperRemoveDuplicates(s []string) []string

去重并移除空字符串。

package main

import (
	"fmt"

	"github.com/2Kil/tkstar"
)

func main() {
	items := []string{"A", "", "B", "A", "C", "B"}
	fmt.Println(tkstar.HelperRemoveDuplicates(items))
}
func LogFile(logFileName string, systemLog bool) (*logger.Logger, error)

初始化日志文件。

package main

import (
	"log"

	"github.com/2Kil/tkstar"
)

func main() {
	l, err := tkstar.LogFile("app", false)
	if err != nil {
		log.Fatal(err)
	}
	defer l.Close()

	l.Info("service started")
}

authorization 包

导入:

import "github.com/2Kil/tkstar/authorization"
func NewClient(code string, pwd ...string) *Client

创建授权客户端。新版活码可同时传密码。

package main

import "github.com/2Kil/tkstar/authorization"

func main() {
	_ = authorization.NewClient("active.clewm.net/q8tDtnl")
	_ = authorization.NewClient("qr61.cn/o78kxB/q8tDtnl", "123456")
}
func (c *Client) GetAccredit() ([]Accredit, error)

按网页解析方式获取授权表格。

package main

import (
	"fmt"
	"log"

	"github.com/2Kil/tkstar/authorization"
)

func main() {
	client := authorization.NewClient("active.clewm.net/q8tDtnl")
	list, err := client.GetAccredit()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(list)
}
func (c *Client) GetAccredit2() ([]Accredit, error)

按接口方式获取新版活码授权数据。

package main

import (
	"fmt"
	"log"

	"github.com/2Kil/tkstar/authorization"
)

func main() {
	client := authorization.NewClient("qr61.cn/o78kxB/q8tDtnl", "123456")
	list, err := client.GetAccredit2()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(list)
}
func (c *Client) CheckAccredit(key string) bool

检查某个序列号是否未过期。

package main

import (
	"fmt"

	"github.com/2Kil/tkstar/authorization"
)

func main() {
	client := authorization.NewClient("qr61.cn/o78kxB/q8tDtnl", "123456")
	fmt.Println(client.CheckAccredit("DEVICE-001"))
}

network 包

导入:

import "github.com/2Kil/tkstar/network"
func NetProxyCurl(proxy, curlBash string) (int, string, error)

使用代理执行 curl 字符串。

package main

import (
	"fmt"
	"log"

	"github.com/2Kil/tkstar/network"
)

func main() {
	code, body, err := network.NetProxyCurl(
		"http://127.0.0.1:7890",
		`curl "https://httpbin.org/get" -H "Accept: application/json"`,
	)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(code)
	fmt.Println(body)
}
func NetParseCurlComd(curlCmd string) (string, string, http.Header, []byte, error)

只解析,不发送请求。

package main

import (
	"fmt"
	"log"

	"github.com/2Kil/tkstar/network"
)

func main() {
	method, urlStr, headers, body, err := network.NetParseCurlComd(
		`curl "https://example.com/api" -X POST -H "Content-Type: application/json" --data-raw "{\"name\":\"tkstar\"}"`,
	)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(method)
	fmt.Println(urlStr)
	fmt.Println(headers.Get("Content-Type"))
	fmt.Println(string(body))
}
func NetCurl(curlBash string) (int, string, error)

不使用代理执行 curl 字符串。

package main

import (
	"fmt"
	"log"

	"github.com/2Kil/tkstar/network"
)

func main() {
	code, body, err := network.NetCurl(`curl "https://httpbin.org/get"`)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(code)
	fmt.Println(body)
}

text 包

导入:

import "github.com/2Kil/tkstar/text"
func TextGetKeyPair(bits int) (*KeyPair, error)

生成 RSA 密钥对。

package main

import (
	"fmt"
	"log"

	"github.com/2Kil/tkstar/text"
)

func main() {
	keyPair, err := text.TextGetKeyPair(1024)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(keyPair.PublicKey.N)
}
func TextEncrypt(pub *PublicKey, plaintext []byte) ([]byte, error)

使用公钥加密。

package main

import (
	"fmt"
	"log"

	"github.com/2Kil/tkstar/text"
)

func main() {
	keyPair, err := text.TextGetKeyPair(1024)
	if err != nil {
		log.Fatal(err)
	}

	cipherText, err := text.TextEncrypt(keyPair.PublicKey, []byte("hello"))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(cipherText)
}
func TextDecrypt(priv *PrivateKey, ciphertext []byte) ([]byte, error)

使用私钥解密。

package main

import (
	"fmt"
	"log"

	"github.com/2Kil/tkstar/text"
)

func main() {
	keyPair, err := text.TextGetKeyPair(1024)
	if err != nil {
		log.Fatal(err)
	}

	cipherText, err := text.TextEncrypt(keyPair.PublicKey, []byte("hello"))
	if err != nil {
		log.Fatal(err)
	}

	plainText, err := text.TextDecrypt(keyPair.PrivateKey, cipherText)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(plainText))
}
func TextVerify(plaintext []byte, decryptedHash []byte) bool

比较明文 SHA-256 和“解密后的哈希值”是否一致。

package main

import (
	"crypto/sha256"
	"fmt"

	"github.com/2Kil/tkstar/text"
)

func main() {
	plain := []byte("hello")
	hash := sha256.Sum256(plain)
	fmt.Println(text.TextVerify(plain, hash[:]))
}
func TextAesEncrypt(plainText, key string) (string, error)

使用 AES-CFB 加密。密钥长度必须是 16、24 或 32 字节。

package main

import (
	"fmt"
	"log"

	"github.com/2Kil/tkstar/text"
)

func main() {
	cipherText, err := text.TextAesEncrypt("hello", "1234567890abcdef")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(cipherText)
}
func TextAesDecrypt(cipherText, key string) (string, error)

解密 TextAesEncrypt 返回的数据。

package main

import (
	"fmt"
	"log"

	"github.com/2Kil/tkstar/text"
)

func main() {
	cipherText, err := text.TextAesEncrypt("hello", "1234567890abcdef")
	if err != nil {
		log.Fatal(err)
	}

	plainText, err := text.TextAesDecrypt(cipherText, "1234567890abcdef")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(plainText)
}

screen 包

导入:

import "github.com/2Kil/tkstar/screen"
func ScreenInit()

初始化透明悬浮窗。

package main

import "github.com/2Kil/tkstar/screen"

func main() {
	go screen.ScreenInit()
	select {}
}
func ScreenUpdateText(text string)

更新悬浮窗文本。

package main

import (
	"time"

	"github.com/2Kil/tkstar/screen"
)

func main() {
	go screen.ScreenInit()
	time.Sleep(time.Second)
	screen.ScreenUpdateText("RUN")
	select {}
}
func ScreenGetText() string

读取当前显示文本。

package main

import (
	"fmt"

	"github.com/2Kil/tkstar/screen"
)

func main() {
	screen.ScreenUpdateText("READY")
	fmt.Println(screen.ScreenGetText())
}

hardware 包

导入:

import "github.com/2Kil/tkstar/hardware"
func SysGetSerialKey() string

生成设备特征码。

package main

import (
	"fmt"

	"github.com/2Kil/tkstar/hardware"
)

func main() {
	fmt.Println(hardware.SysGetSerialKey())
}
func KeyIsPress(keyName string) bool

检测某个键当前是否按下。

package main

import (
	"fmt"
	"time"

	"github.com/2Kil/tkstar/hardware"
)

func main() {
	for {
		if hardware.KeyIsPress("F8") {
			fmt.Println("F8 pressed")
			return
		}
		time.Sleep(50 * time.Millisecond)
	}
}

edge 包

导入:

import tkEdge "github.com/2Kil/tkstar/edge"
func Run(urlPath string, msedgePath ...string)

启动带界面的 Edge。

package main

import tkEdge "github.com/2Kil/tkstar/edge"

func main() {
	tkEdge.Run("https://example.com")
	select {}
}
func RunCli(urlPath string, msedgePath ...string)

启动无头 Edge。

package main

import tkEdge "github.com/2Kil/tkstar/edge"

func main() {
	tkEdge.RunCli("https://example.com")
	select {}
}
func Stop()

停止当前浏览器上下文。

package main

import (
	"time"

	tkEdge "github.com/2Kil/tkstar/edge"
)

func main() {
	tkEdge.Run("https://example.com")
	time.Sleep(5 * time.Second)
	tkEdge.Stop()
}
func LoadUrl(targetURL string)

让已启动的浏览器跳转到新地址。

package main

import (
	"time"

	tkEdge "github.com/2Kil/tkstar/edge"
)

func main() {
	tkEdge.Run("https://example.com")
	time.Sleep(3 * time.Second)
	tkEdge.LoadUrl("https://httpbin.org/get")
	select {}
}
func GetUrl() (string, error)

读取当前页面地址。

package main

import (
	"fmt"
	"log"
	"time"

	tkEdge "github.com/2Kil/tkstar/edge"
)

func main() {
	tkEdge.Run("https://example.com")
	time.Sleep(3 * time.Second)

	urlStr, err := tkEdge.GetUrl()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(urlStr)
}
func GetCookies() (map[string]string, error)

获取当前页面 Cookie。

package main

import (
	"fmt"
	"log"
	"time"

	tkEdge "github.com/2Kil/tkstar/edge"
)

func main() {
	tkEdge.Run("https://example.com")
	time.Sleep(3 * time.Second)

	cookies, err := tkEdge.GetCookies()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(cookies)
}
func GetCookiesAll() (map[string]string, error)

从 storage 读取全部 Cookie。

package main

import (
	"fmt"
	"log"
	"time"

	tkEdge "github.com/2Kil/tkstar/edge"
)

func main() {
	tkEdge.Run("https://example.com")
	time.Sleep(3 * time.Second)

	cookies, err := tkEdge.GetCookiesAll()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(cookies)
}
func GetReq(key string) string

获取最近一次匹配请求中的请求头。

package main

import (
	"fmt"
	"time"

	tkEdge "github.com/2Kil/tkstar/edge"
)

func main() {
	tkEdge.Run("https://httpbin.org/headers")
	time.Sleep(5 * time.Second)
	fmt.Println(tkEdge.GetReq("User-Agent"))
}
func GetRes(key string) string

获取最近一次匹配响应中的响应头。

package main

import (
	"fmt"
	"time"

	tkEdge "github.com/2Kil/tkstar/edge"
)

func main() {
	tkEdge.Run("https://httpbin.org/get")
	time.Sleep(5 * time.Second)
	fmt.Println(tkEdge.GetRes("content-type"))
}
func GetUrlQuery(key string) string

获取最近一次匹配请求 URL 里的查询参数。

package main

import (
	"fmt"
	"time"

	tkEdge "github.com/2Kil/tkstar/edge"
)

func main() {
	tkEdge.Run("https://httpbin.org/get?token=123")
	time.Sleep(5 * time.Second)
	fmt.Println(tkEdge.GetUrlQuery("token"))
}
func Clear()

清理最近一次保存的请求/响应信息。

package main

import (
	"fmt"
	"time"

	tkEdge "github.com/2Kil/tkstar/edge"
)

func main() {
	tkEdge.Run("https://httpbin.org/get?token=123")
	time.Sleep(5 * time.Second)

	fmt.Println(tkEdge.GetUrlQuery("token"))
	tkEdge.Clear()
	fmt.Println(tkEdge.GetUrlQuery("token"))
}

许可证

本项目采用 LICENSE 中定义的许可证。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildTime

func BuildTime() string

BuildTime 输出并返回当前可执行文件的最后修改时间。 注意:这并不是准确的编译时间,而是文件的最后写入时间。 return: 时间字符串,格式 "06.0102.1504"

func CheckErr

func CheckErr(err error, errString ...string) bool

CheckErr 错误检测辅助函数。 如果 err 不为 nil,打印日志并返回 false;否则返回 true。 param: err 错误信息 param: errString 自定义错误提示前缀 return: 无错误 true, 有错误 false

func HelperRemoveDuplicates

func HelperRemoveDuplicates(s []string) []string

HelperRemoveDuplicates 对字符串切片进行去重和去空处理。 param: s 待处理的切片 return: 处理后的切片

func IsDebug

func IsDebug() bool

IsDebug 判断当前环境是否为 Debug 模式。 通过检查执行文件路径中是否包含 "Temp" 和 "go-build" 来判断(通常适用于 go run)。 return: true: Debug模式, false: Release模式

func LogFile added in v1.0.2

func LogFile(logFileName string, systemLog bool) (*logger.Logger, error)

LogFile 初始化日志系统,支持写入文件和系统日志。 param: logFileName 日志文件名前缀 (不含 .log) param: systemLog 是否写入系统事件日志 (Windows Event Log / Syslog) return: Logger 实例指针, 错误信息

func RandAtomic

func RandAtomic(max int) int

RandAtomic 生成 [0, max) 范围内的随机整数。 param: max 最大值(不包含) return: 随机数

func RandAtomicRadius

func RandAtomicRadius(min, max int) int

RandAtomicRadius 生成 [min, max] 范围内的随机整数。 param: min 最小值 param: max 最大值 return: 随机数

Types

This section is empty.

Directories

Path Synopsis
* @Author: 2Kil * @Date: 2026-01-07 18:48:47 * @LastEditors: 2Kil * @LastEditTime: 2026-01-07 22:37:53 * @Description:简单授权
* @Author: 2Kil * @Date: 2026-01-07 18:48:47 * @LastEditors: 2Kil * @LastEditTime: 2026-01-07 22:37:53 * @Description:简单授权
* @Author: 2Kil * @Date: 2025-09-28 13:46:43 * @LastEditors: 2Kil * @LastEditTime: 2025-12-15 11:44:30 * @Description:网络相关
* @Author: 2Kil * @Date: 2025-09-28 13:46:43 * @LastEditors: 2Kil * @LastEditTime: 2025-12-15 11:44:30 * @Description:网络相关

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址