Skip to content
This repository has been archived by the owner on Jan 6, 2021. It is now read-only.

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
zsakvo committed May 11, 2019
0 parents commit 3264b30
Show file tree
Hide file tree
Showing 12 changed files with 616 additions and 0 deletions.
60 changes: 60 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

# Created by https://www.gitignore.io/api/go,code,macos
# Edit at https://www.gitignore.io/?templates=go,code,macos

### Code ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

### Go ###
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

### Go Patch ###
/vendor/
/Godeps/

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# End of https://www.gitignore.io/api/go,code,macos
conf.ini
/bin
7 changes: 7 additions & 0 deletions check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

func check(e error) {
if e != nil {
panic(e)
}
}
39 changes: 39 additions & 0 deletions clear.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"os"
"os/exec"
"runtime"
)

var clearMap map[string]func() //create a map for storing clear funcs

func init() {
clearMap = make(map[string]func()) //Initialize it
clearMap["linux"] = func() {
cmd := exec.Command("clear") //Linux example, its tested
cmd.Stdout = os.Stdout
cmd.Run()
}
clearMap["darwin"] = func() {
cmd := exec.Command("clear") //Linux example, its tested
cmd.Stdout = os.Stdout
cmd.Run()
}
clearMap["windows"] = func() {
cmd := exec.Command("cls") //Windows example it is untested, but I think its working
cmd.Stdout = os.Stdout
cmd.Run()
}
}

//clear 清屏
func clear() {
value, ok := clearMap[runtime.GOOS] //runtime.GOOS -> linux, windows, darwin etc.
if ok { //if we defined a clear func for that platform:
value() //we execute it
} else { //unsupported platform
panic("Your platform is unsupported! I can't clear terminal screen :(")
}
return
}
29 changes: 29 additions & 0 deletions cstruct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"github.com/tidwall/gjson"
)

//Token1 struct
type Token1 struct {
ReaderID string
LoginToken string
Account string
AppVersion string
}

//Book1 struct
type Book1 struct {
BookName string
Division string
Volumes []gjson.Result
VolumeNum int
ChapterIDs []gjson.Result
ChapterNum int
}

//Settings1 struct
type Settings1 struct {
TmpPath string
OutputPath string
}
64 changes: 64 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"crypto/aes"
"crypto/cipher"
"crypto/sha256"
"encoding/base64"
)

var (
//IV 偏移量
IV = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
test = "c1SR02T7X+xmq37zfs0U8NAj73eedAs3tnXMQKDNUPlI2vcaNRXpKA3JktMoffp3EYPCsvCjzeCJUynjDISbNP4D5HjaCp6tMrOsBBfQzVI="
)

//SHA256 sha256编码
func SHA256(data []byte) []byte {
ret := sha256.Sum256(data)
return ret[:]
}

//Base64Decode Base64解码
func Base64Decode(encoded string) ([]byte, error) {
decoded, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
return decoded, err
}
return decoded, nil
}

//LoadKey 读取解密密钥
func LoadKey(EncryptKey string) []byte {
Key := SHA256([]byte(EncryptKey))
return Key[:32]
}

//AESDecrypt AES解密
func AESDecrypt(EncryptKey string, ciphertext []byte) []byte {
key := LoadKey(EncryptKey)
block, err := aes.NewCipher(key)
check(err)
// Generally use first 16 bytes cipher text as IV
// in this case they use 16 bytes 0x00
blockModel := cipher.NewCBCDecrypter(block, IV)
plainText := make([]byte, len(ciphertext))
blockModel.CryptBlocks(plainText, ciphertext)
plainText = PKCS7UnPadding(plainText)
return plainText
}

//PKCS7UnPadding 对齐
func PKCS7UnPadding(plainText []byte) []byte {
length := len(plainText)
unpadding := int(plainText[length-1])
return plainText[:(length - unpadding)]
}

//GetContent 入口函数
func decode(str string, EncryptKey string) string {
decoded, err := Base64Decode(str)
check(err)
raw := AESDecrypt(EncryptKey, decoded)
return string(raw)
}
59 changes: 59 additions & 0 deletions files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"io/ioutil"
"os"
"path/filepath"
)

func isFileExist(filePath string) bool {
_, err := os.Stat(filePath) //os.Stat获取文件信息
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}

//合并缓存
func mergeTemp() bool {
var content []byte
if !isFileExist(path.out) {
os.MkdirAll(path.out, os.ModePerm)
}
outPath := path.out + "/" + bookName + ".txt"
tmpPath := path.tmp + "/" + bookName + "/"
for _, cid := range chapterIDs {
d, err := ioutil.ReadFile(tmpPath + cid.String() + ".txt")
if err == nil {
content = append(content, d...)
}
}
err1 := ioutil.WriteFile(outPath, content, 0644)
if err1 != nil {
return false
}
return true
}

//移除缓存
func destoryTemp() error {
d, err := os.Open(path.tmp)
if err != nil {
return err
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
return err
}
for _, name := range names {
err = os.RemoveAll(filepath.Join(path.tmp, name))
if err != nil {
return err
}
}
return nil
}
88 changes: 88 additions & 0 deletions getConfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package main

import (
"fmt"
"os"

"github.com/Unknwon/goconfig"
)

//读取配置文件
func getConfig() {
if isFileExist("conf.ini") {
cfg, err := goconfig.LoadConfigFile("conf.ini")
if err != nil {
os.Remove("conf.ini")
initSettings()
}
config = cfg
} else {
initSettings()
fmt.Println("已初始化 conf.ini 配置文件,请按提示填写后运行本程序")
os.Exit(1)
}
}

//获取 section
func getSection(value string) map[string]string {
account, err := config.GetSection(value)
if err != nil {
initSettings()
}
return account
}

func getAccountSettings() accountSettings {
accountMap := getSection("account")
var aStruct accountSettings
aStruct.username = accountMap["username"]
aStruct.password = accountMap["password"]
if len(aStruct.username) == 0 {
fmt.Println("请于 conf.ini 中填写用户名")
os.Exit(1)
}
if len(aStruct.password) == 0 {
fmt.Println("请于 conf.ini 中填写密码")
os.Exit(1)
}
return aStruct
}

func getPathSettings() pathSettings {
pathMap := getSection("path")
var aStruct pathSettings
aStruct.tmp = pathMap["tmp"]
aStruct.out = pathMap["out"]
if len(aStruct.tmp) == 0 {
fmt.Println("请于 conf.ini 中填写临时目录路径")
os.Exit(1)
}
if len(aStruct.out) == 0 {
fmt.Println("请于 conf.ini 中填写输出目录路径")
os.Exit(1)
}
return aStruct
}

func getToken() string {
tokenMap := getSection("token")
return tokenMap["token"]
}

func initSettings() {
os.Create("conf.ini")
cfg, err := goconfig.LoadConfigFile("conf.ini")
check(err)
cfg.SetValue("account", "username", "")
cfg.SetKeyComments("account", "username", "# 用户名,必填")
cfg.SetValue("account", "password", "")
cfg.SetKeyComments("account", "password", "# 密码,必填")
cfg.SetValue("token", "token", "")
cfg.SetKeyComments("token", "token", "# 自动生成,请勿修改")
cfg.SetValue("path", "tmp", "tmp")
cfg.SetKeyComments("path", "tmp", "# 临时目录,必填")
cfg.SetValue("path", "out", "output")
cfg.SetKeyComments("path", "out", "# 输出目录,必填")
err1 := goconfig.SaveConfigFile(cfg, "conf.ini")
check(err1)
}
Loading

0 comments on commit 3264b30

Please sign in to comment.