CLI email sender
This commit is contained in:
384
vendor/local/encryptor/encryptor.go
vendored
Executable file
384
vendor/local/encryptor/encryptor.go
vendored
Executable file
@@ -0,0 +1,384 @@
|
||||
package encryptor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/openpgp"
|
||||
"golang.org/x/crypto/openpgp/packet"
|
||||
// for openpgp
|
||||
_ "golang.org/x/crypto/ripemd160"
|
||||
)
|
||||
|
||||
const keylen = 32
|
||||
|
||||
// Encryptor interface
|
||||
type Encryptor interface {
|
||||
Encrypt(string) string
|
||||
Decrypt(string) string
|
||||
SetPublic(string)
|
||||
GetPublic() string
|
||||
SetSymmetric(string)
|
||||
|
||||
FromFiles(string, string) error
|
||||
ToFiles(string, string) error
|
||||
}
|
||||
|
||||
type encryptor struct {
|
||||
entity *openpgp.Entity
|
||||
writing *openpgp.Entity
|
||||
symmetric string
|
||||
b64 bool
|
||||
}
|
||||
|
||||
func (e *encryptor) SetPublic(pub string) {
|
||||
reader := b64dec(pub)
|
||||
e.writing = entityFromPackets(nil, strToPubPack(reader))
|
||||
}
|
||||
|
||||
func (e *encryptor) GetPublic() string {
|
||||
bufpub := new(bytes.Buffer)
|
||||
e.writing.Serialize(bufpub)
|
||||
bufPu, err := ioutil.ReadAll(bufpub)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b64enc(string(bufPu))
|
||||
}
|
||||
|
||||
func (e *encryptor) setPrivate(pri string) {
|
||||
readerPri := b64dec(pri)
|
||||
readerPub := b64dec(e.GetPublic())
|
||||
e.entity = entityFromPackets(strToPriPack(readerPri), strToPubPack(readerPub))
|
||||
}
|
||||
|
||||
func (e *encryptor) getPrivate() string {
|
||||
bufpri := new(bytes.Buffer)
|
||||
e.entity.SerializePrivate(bufpri, nil)
|
||||
bufPr, err := ioutil.ReadAll(bufpri)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b64enc(string(bufPr))
|
||||
}
|
||||
|
||||
// NewKeyPair generates a PGP private-public key pair.
|
||||
func NewKeyPair() (string, string) {
|
||||
entity, err := openpgp.NewEntity("", "", "", nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
en := &encryptor{
|
||||
entity: entity,
|
||||
writing: entity,
|
||||
}
|
||||
return en.getPrivate(), en.GetPublic()
|
||||
}
|
||||
|
||||
func (e *encryptor) getPubPacket() *packet.PublicKey {
|
||||
return e.writing.PrimaryKey
|
||||
}
|
||||
|
||||
func (e *encryptor) getPriPacket() *packet.PrivateKey {
|
||||
return e.entity.PrivateKey
|
||||
}
|
||||
|
||||
func entityFromPackets(priKey *packet.PrivateKey, pubKey *packet.PublicKey) *openpgp.Entity {
|
||||
config := packet.Config{
|
||||
DefaultHash: crypto.SHA256,
|
||||
DefaultCipher: packet.CipherAES256,
|
||||
DefaultCompressionAlgo: packet.CompressionZLIB,
|
||||
CompressionConfig: &packet.CompressionConfig{
|
||||
Level: 9,
|
||||
},
|
||||
RSABits: 4096,
|
||||
}
|
||||
|
||||
currentTime := config.Now()
|
||||
uid := packet.NewUserId("", "", "")
|
||||
|
||||
e := openpgp.Entity{
|
||||
PrimaryKey: pubKey,
|
||||
PrivateKey: priKey,
|
||||
Identities: make(map[string]*openpgp.Identity),
|
||||
}
|
||||
isPrimaryID := false
|
||||
|
||||
e.Identities[uid.Id] = &openpgp.Identity{
|
||||
Name: uid.Name,
|
||||
UserId: uid,
|
||||
SelfSignature: &packet.Signature{
|
||||
CreationTime: currentTime,
|
||||
SigType: packet.SigTypePositiveCert,
|
||||
PubKeyAlgo: packet.PubKeyAlgoRSA,
|
||||
Hash: config.Hash(),
|
||||
IsPrimaryId: &isPrimaryID,
|
||||
FlagsValid: true,
|
||||
FlagSign: true,
|
||||
FlagCertify: true,
|
||||
IssuerKeyId: &e.PrimaryKey.KeyId,
|
||||
},
|
||||
}
|
||||
|
||||
keyLifetimeSecs := uint32(86400 * 365)
|
||||
|
||||
e.Subkeys = make([]openpgp.Subkey, 1)
|
||||
e.Subkeys[0] = openpgp.Subkey{
|
||||
PublicKey: pubKey,
|
||||
PrivateKey: priKey,
|
||||
Sig: &packet.Signature{
|
||||
CreationTime: currentTime,
|
||||
SigType: packet.SigTypeSubkeyBinding,
|
||||
PubKeyAlgo: packet.PubKeyAlgoRSA,
|
||||
Hash: config.Hash(),
|
||||
PreferredHash: []uint8{8}, // SHA-256
|
||||
FlagsValid: true,
|
||||
FlagEncryptStorage: true,
|
||||
FlagEncryptCommunications: true,
|
||||
IssuerKeyId: &e.PrimaryKey.KeyId,
|
||||
KeyLifetimeSecs: &keyLifetimeSecs,
|
||||
},
|
||||
}
|
||||
return &e
|
||||
|
||||
}
|
||||
|
||||
func strToPack(str string) interface{} {
|
||||
reader := b64dec(str)
|
||||
KeyRead := bytes.NewReader([]byte(reader))
|
||||
PacketReader := packet.NewReader(KeyRead)
|
||||
pkt, err := PacketReader.Next()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return pkt
|
||||
}
|
||||
|
||||
func strToPriPack(private string) *packet.PrivateKey {
|
||||
priKeyPack, ok := strToPack(private).(*packet.PrivateKey)
|
||||
if !ok {
|
||||
panic(ok)
|
||||
}
|
||||
return priKeyPack
|
||||
}
|
||||
|
||||
func strToPubPack(public string) *packet.PublicKey {
|
||||
pubKeyPack, ok := strToPack(public).(*packet.PublicKey)
|
||||
if !ok {
|
||||
panic(ok)
|
||||
}
|
||||
return pubKeyPack
|
||||
}
|
||||
|
||||
// NewEncryptor function
|
||||
func NewEncryptor(private, public string, b64 ...bool) *encryptor {
|
||||
backupPri, backupPub := NewKeyPair()
|
||||
if private == "" {
|
||||
private = backupPri
|
||||
}
|
||||
if public == "" {
|
||||
public = backupPub
|
||||
}
|
||||
priKeyPack := strToPriPack(private)
|
||||
pubKeyPack := strToPubPack(public)
|
||||
|
||||
b64payloads := len(b64) > 0 && b64[0]
|
||||
|
||||
return &encryptor{
|
||||
entity: entityFromPackets(priKeyPack, pubKeyPack),
|
||||
writing: entityFromPackets(nil, pubKeyPack),
|
||||
b64: b64payloads,
|
||||
}
|
||||
}
|
||||
|
||||
func b64enc(msg string) string {
|
||||
return base64.StdEncoding.EncodeToString([]byte(msg))
|
||||
}
|
||||
|
||||
func b64dec(msg string) string {
|
||||
decMe, err := base64.StdEncoding.DecodeString(msg)
|
||||
if err != nil {
|
||||
return msg
|
||||
}
|
||||
return string(decMe)
|
||||
}
|
||||
|
||||
func (e *encryptor) SetSymmetric(key string) {
|
||||
e.symmetric = key
|
||||
}
|
||||
|
||||
func (e *encryptor) Encrypt(msg string) string {
|
||||
if e.symmetric != "" {
|
||||
return e.symEncrypt(msg)
|
||||
}
|
||||
return e.asymEncrypt(msg)
|
||||
}
|
||||
|
||||
func (e *encryptor) Decrypt(msg string) string {
|
||||
if e.symmetric != "" {
|
||||
return e.symDecrypt(msg)
|
||||
}
|
||||
return e.asymDecrypt(msg)
|
||||
}
|
||||
|
||||
func fixKey(key string) []byte {
|
||||
return []byte(strings.Repeat("minkey"+string(key), keylen)[:keylen])
|
||||
}
|
||||
|
||||
func pad(src []byte) []byte {
|
||||
padding := aes.BlockSize - len(src)%aes.BlockSize
|
||||
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||
return append(src, padtext...)
|
||||
}
|
||||
|
||||
func unpad(src []byte) ([]byte, error) {
|
||||
length := len(src)
|
||||
unpadding := int(src[length-1])
|
||||
|
||||
if unpadding > length {
|
||||
return nil, errors.New("unpad error. This could happen when incorrect encryption key is used")
|
||||
}
|
||||
|
||||
return src[:(length - unpadding)], nil
|
||||
}
|
||||
|
||||
func (e *encryptor) symEncrypt(smsg string) string {
|
||||
key := fixKey(e.symmetric)
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
msg := pad([]byte(smsg))
|
||||
ciphertext := make([]byte, aes.BlockSize+len(msg))
|
||||
iv := ciphertext[:aes.BlockSize]
|
||||
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
cfb := cipher.NewCFBEncrypter(block, iv)
|
||||
cfb.XORKeyStream(ciphertext[aes.BlockSize:], msg)
|
||||
if e.b64 {
|
||||
return b64enc(string(ciphertext))
|
||||
}
|
||||
return string(ciphertext)
|
||||
}
|
||||
|
||||
func (e *encryptor) symDecrypt(msg string) string {
|
||||
payload := []byte(msg)
|
||||
if e.b64 {
|
||||
payload = []byte(b64dec(msg))
|
||||
}
|
||||
if len(payload) == 0 {
|
||||
return ""
|
||||
}
|
||||
key := fixKey(e.symmetric)
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
if (len(payload) % aes.BlockSize) != 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
iv := payload[:aes.BlockSize]
|
||||
bmsg := payload[aes.BlockSize:]
|
||||
|
||||
cfb := cipher.NewCFBDecrypter(block, iv)
|
||||
cfb.XORKeyStream(bmsg, bmsg)
|
||||
|
||||
b, _ := unpad(bmsg)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// Encrypt function
|
||||
func (e *encryptor) asymEncrypt(msg string) string {
|
||||
// encrypt string
|
||||
buf := new(bytes.Buffer)
|
||||
w, err := openpgp.Encrypt(buf, []*openpgp.Entity{e.writing}, nil, nil, nil)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
_, err = w.Write([]byte(msg))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
err = w.Close()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
// Encode to b64
|
||||
return b64enc(string(buf.Bytes()))
|
||||
}
|
||||
|
||||
// Decrypt function
|
||||
func (e *encryptor) asymDecrypt(msg string) string {
|
||||
// Decode the b64 string
|
||||
decMe := b64dec(msg)
|
||||
KeyRead := bytes.NewReader([]byte(decMe))
|
||||
|
||||
md, err := openpgp.ReadMessage(KeyRead, openpgp.EntityList([]*openpgp.Entity{e.entity}), nil, nil)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
plaintext, err := ioutil.ReadAll(md.UnverifiedBody)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return string(plaintext)
|
||||
}
|
||||
|
||||
func (e *encryptor) FromFiles(privatePath, publicPath string) error {
|
||||
priRaw, err := ioutil.ReadFile(privatePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pubRaw, err := ioutil.ReadFile(publicPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d := NewEncryptor(string(priRaw), string(pubRaw))
|
||||
|
||||
v := d.Encrypt("Hello")
|
||||
if len(v) == 0 {
|
||||
return errors.New("bad public-private key file pair")
|
||||
}
|
||||
v = d.Decrypt(v)
|
||||
if len(v) == 0 {
|
||||
return errors.New("bad public-private key file pair")
|
||||
}
|
||||
e.entity = d.entity
|
||||
e.writing = d.entity
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *encryptor) ToFiles(privatePath, publicPath string) error {
|
||||
d := &encryptor{
|
||||
writing: e.entity,
|
||||
entity: e.entity,
|
||||
}
|
||||
if err := os.MkdirAll(path.Dir(privatePath), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ioutil.WriteFile(privatePath, []byte(d.getPrivate()), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(path.Dir(publicPath), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile(publicPath, []byte(d.GetPublic()), os.ModePerm)
|
||||
}
|
||||
102
vendor/local/encryptor/first
vendored
Executable file
102
vendor/local/encryptor/first
vendored
Executable file
@@ -0,0 +1,102 @@
|
||||
package encryptor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
||||
"golang.org/x/crypto/openpgp"
|
||||
_ "golang.org/x/crypto/ripemd160"
|
||||
)
|
||||
|
||||
// Encryptor interface
|
||||
type Encryptor interface {
|
||||
Encrypt(string) string
|
||||
Decrypt() string
|
||||
}
|
||||
|
||||
type encryptor struct {
|
||||
entity *openpgp.Entity
|
||||
}
|
||||
|
||||
// NewEncryptor function
|
||||
func NewEncryptor(private, public string) *encryptor {
|
||||
entity, err := openpgp.NewEntity("", "", "", nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
entity.PrimaryKey.PubKeyAlgo
|
||||
entity.PrimaryKey.PublicKey
|
||||
entity.PrivateKey.PubKeyAlgo
|
||||
entity.PrivateKey.PrivateKey
|
||||
fmt.Println(fmt.Sprintf("%T", entity.PrimaryKey.PublicKey))
|
||||
bufpri := new(bytes.Buffer)
|
||||
entity.SerializePrivate(bufpri, nil)
|
||||
bufpub := new(bytes.Buffer)
|
||||
entity.Serialize(bufpub)
|
||||
bufPr, _ := b64enc(bufpri)
|
||||
bufPu, _ := b64enc(bufpub)
|
||||
fmt.Printf("PRIVATE: %v\nPUBLIC: %v\n", bufPr[10:20], bufPu[10:20])
|
||||
return &encryptor{
|
||||
entity: entity,
|
||||
}
|
||||
}
|
||||
|
||||
func b64enc(msg io.Reader) (string, error) {
|
||||
bytes, err := ioutil.ReadAll(msg)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.StdEncoding.EncodeToString(bytes), nil
|
||||
}
|
||||
|
||||
func b64dec(msg string) (io.Reader, error) {
|
||||
decMe, err := base64.StdEncoding.DecodeString(msg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bytes.NewBuffer(decMe), nil
|
||||
}
|
||||
|
||||
// Encrypt function
|
||||
func (e *encryptor) Encrypt(msg string) (string, error) {
|
||||
// encrypt string
|
||||
buf := new(bytes.Buffer)
|
||||
w, err := openpgp.Encrypt(buf, []*openpgp.Entity{e.entity}, nil, nil, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
_, err = w.Write([]byte(msg))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
err = w.Close()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// Encode to base64
|
||||
return b64enc(buf)
|
||||
}
|
||||
|
||||
// Decrypt function
|
||||
func (e *encryptor) Decrypt(msg string) (string, error) {
|
||||
// Decode the base64 string
|
||||
decMe, err := b64dec(msg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
md, err := openpgp.ReadMessage(decMe, openpgp.EntityList([]*openpgp.Entity{e.entity}), nil, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
plaintext, err := ioutil.ReadAll(md.UnverifiedBody)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(plaintext), nil
|
||||
}
|
||||
156
vendor/local/encryptor/second
vendored
Executable file
156
vendor/local/encryptor/second
vendored
Executable file
@@ -0,0 +1,156 @@
|
||||
package encryptor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/jchavannes/go-pgp/pgp"
|
||||
"golang.org/x/crypto/openpgp"
|
||||
"golang.org/x/crypto/openpgp/armor"
|
||||
)
|
||||
|
||||
// Encryptor interface
|
||||
type Encryptor interface {
|
||||
Encrypt(string) string
|
||||
Decrypt() string
|
||||
}
|
||||
|
||||
type encryptor struct {
|
||||
entity *openpgp.Entity
|
||||
}
|
||||
|
||||
type readerWriter struct {
|
||||
b []byte
|
||||
}
|
||||
|
||||
func (rw *readerWriter) Write(b []byte) (int, error) {
|
||||
rw.b = b[:]
|
||||
return len(b), io.EOF
|
||||
}
|
||||
|
||||
func (rw *readerWriter) Read(b []byte) (int, error) {
|
||||
b = rw.b[:]
|
||||
return len(b), io.EOF
|
||||
}
|
||||
|
||||
func NewKeyPair() (string, string) {
|
||||
entity, err := openpgp.NewEntity("a", "b", "c@d.e", nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Sign all the identities
|
||||
for _, id := range entity.Identities {
|
||||
err := id.SelfSignature.SignUserId(id.UserId.Id, entity.PrimaryKey, entity.PrivateKey, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
rw := &readerWriter{b: make([]byte, 5000)}
|
||||
b := make([]byte, 5000)
|
||||
|
||||
priW, err := armor.Encode(rw, openpgp.PrivateKeyType, nil)
|
||||
if err != io.EOF {
|
||||
fmt.Println(priW, rw)
|
||||
panic(err)
|
||||
}
|
||||
if priW != nil {
|
||||
defer priW.Close()
|
||||
entity.SerializePrivate(priW, nil)
|
||||
}
|
||||
rw.Read(b)
|
||||
pri := string(b)
|
||||
|
||||
pubW, err := armor.Encode(rw, openpgp.PublicKeyType, nil)
|
||||
if err != io.EOF {
|
||||
panic(err)
|
||||
}
|
||||
rw.Read(b)
|
||||
pub := string(b)
|
||||
if pubW != nil {
|
||||
defer pubW.Close()
|
||||
entity.Serialize(pubW)
|
||||
}
|
||||
fmt.Println(pri, pub)
|
||||
|
||||
return pri, pub
|
||||
}
|
||||
|
||||
// NewEncryptor function
|
||||
func NewEncryptor(private, public string) *encryptor {
|
||||
pubKeyPack, err := pgp.GetPublicKeyPacket([]byte(public))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
priKeyPack, err := pgp.GetPrivateKeyPacket([]byte(private))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
entity, err := openpgp.NewEntity("", "", "", nil)
|
||||
entity.PrimaryKey = pubKeyPack
|
||||
entity.PrivateKey = priKeyPack
|
||||
return &encryptor{
|
||||
entity: entity,
|
||||
}
|
||||
}
|
||||
|
||||
func b64enc(msg io.Reader) (string, error) {
|
||||
bytes, err := ioutil.ReadAll(msg)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.StdEncoding.EncodeToString(bytes), nil
|
||||
}
|
||||
|
||||
func b64dec(msg string) (io.Reader, error) {
|
||||
decMe, err := base64.StdEncoding.DecodeString(msg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bytes.NewBuffer(decMe), nil
|
||||
}
|
||||
|
||||
// Encrypt function
|
||||
func (e *encryptor) Encrypt(msg string) (string, error) {
|
||||
// encrypt string
|
||||
buf := new(bytes.Buffer)
|
||||
w, err := openpgp.Encrypt(buf, []*openpgp.Entity{e.entity}, nil, nil, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
_, err = w.Write([]byte(msg))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
err = w.Close()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// Encode to base64
|
||||
return b64enc(buf)
|
||||
}
|
||||
|
||||
// Decrypt function
|
||||
func (e *encryptor) Decrypt(msg string) (string, error) {
|
||||
// Decode the base64 string
|
||||
decMe, err := b64dec(msg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
md, err := openpgp.ReadMessage(decMe, openpgp.EntityList([]*openpgp.Entity{e.entity}), nil, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
plaintext, err := ioutil.ReadAll(md.UnverifiedBody)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(plaintext), nil
|
||||
}
|
||||
98
vendor/local/logger/logger.go
vendored
Executable file
98
vendor/local/logger/logger.go
vendored
Executable file
@@ -0,0 +1,98 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
var outfile *os.File
|
||||
var outfileB *os.File
|
||||
|
||||
var lastWasReturn = false
|
||||
|
||||
var ErrCantConfigLog = errors.New("cannot configure log")
|
||||
|
||||
func Config(path string) error {
|
||||
if path == "" {
|
||||
return nil
|
||||
}
|
||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
return ErrCantConfigLog
|
||||
}
|
||||
outfile = f
|
||||
return nil
|
||||
}
|
||||
|
||||
func Logf(args ...interface{}) {
|
||||
if outfile == nil {
|
||||
outfile = os.Stderr
|
||||
}
|
||||
defer outfile.Sync()
|
||||
if len(args) == 0 {
|
||||
fmt.Fprintln(outfile, "")
|
||||
return
|
||||
}
|
||||
v := fmt.Sprintf(args[0].(string), args[1:]...)
|
||||
if v[len(v)-1] == '\n' {
|
||||
v = v[:len(v)-1]
|
||||
}
|
||||
thisWasReturn := v[0] == '\r'
|
||||
if lastWasReturn && !thisWasReturn {
|
||||
fmt.Println()
|
||||
}
|
||||
if thisWasReturn {
|
||||
fmt.Fprintf(outfile, "\r%s%v", prefix(), v[1:])
|
||||
} else {
|
||||
fmt.Fprintf(outfile, "%s%v\n", prefix(), v)
|
||||
}
|
||||
lastWasReturn = thisWasReturn
|
||||
}
|
||||
|
||||
func Fatalf(args ...interface{}) {
|
||||
Logf(args...)
|
||||
if len(args) > 0 {
|
||||
panic(args[0])
|
||||
} else {
|
||||
panic("Panic from logger.fatal()")
|
||||
}
|
||||
}
|
||||
|
||||
func Fatal(args ...interface{}) {
|
||||
Log(args...)
|
||||
if len(args) > 0 {
|
||||
panic(args[0])
|
||||
} else {
|
||||
panic("Panic from logger.fatal()")
|
||||
}
|
||||
}
|
||||
|
||||
func Log(args ...interface{}) {
|
||||
if lastWasReturn {
|
||||
fmt.Println()
|
||||
}
|
||||
if outfile == nil {
|
||||
outfile = os.Stderr
|
||||
}
|
||||
v := fmt.Sprintf("%v", args)
|
||||
v = v[1 : len(v)-1]
|
||||
fmt.Fprintf(outfile, "%s%v\n", prefix(), v)
|
||||
outfile.Sync()
|
||||
lastWasReturn = false
|
||||
}
|
||||
|
||||
func LogB(args ...interface{}) {
|
||||
if outfileB == nil {
|
||||
outfileB = os.Stderr
|
||||
}
|
||||
v := fmt.Sprintf("%v", args)
|
||||
v = v[1 : len(v)-1]
|
||||
fmt.Fprintf(outfileB, "%s%v\n", prefix(), v)
|
||||
outfileB.Sync()
|
||||
}
|
||||
|
||||
func prefix() string {
|
||||
return time.Now().Format("20060102-150405") + " : "
|
||||
}
|
||||
77
vendor/local/system/sysconf/conf.json
vendored
Executable file
77
vendor/local/system/sysconf/conf.json
vendored
Executable file
@@ -0,0 +1,77 @@
|
||||
[
|
||||
{
|
||||
"Name": "watchmans",
|
||||
"Port": "9988,9998"
|
||||
},
|
||||
{
|
||||
"Name": "ytubespdrun",
|
||||
"Port": "29687",
|
||||
"Log": "/volume1/video/Bel/YouTube/Speedrun/"
|
||||
},
|
||||
{
|
||||
"Name": "emailmonitor",
|
||||
"Log": "breellocaldev@gmail.com",
|
||||
"Timeout": "15m"
|
||||
},
|
||||
{
|
||||
"Name": "vpntorfeed",
|
||||
"Port": "29688",
|
||||
"DB": "magnets",
|
||||
"IP2": "http://192.168.0.86:9091/transmission/rpc",
|
||||
"Pub": "X-Transmission-Session-Id"
|
||||
},
|
||||
{
|
||||
"Name": "broadcaster",
|
||||
"Timeout": "5s",
|
||||
"IP": "localhost"
|
||||
},
|
||||
{
|
||||
"Name": "jbt-serve",
|
||||
"Timeout": "5s",
|
||||
"IP": "localhost",
|
||||
"Port": "59998"
|
||||
},
|
||||
{
|
||||
"Name": "jbt-client",
|
||||
"Timeout": "5s",
|
||||
"IP": "localhost"
|
||||
},
|
||||
{
|
||||
"Name": "nat-serve",
|
||||
"Timeout": "5s",
|
||||
"IP": "localhost",
|
||||
"Port": "59997"
|
||||
},
|
||||
{
|
||||
"Name": "Coordinator",
|
||||
"Port": "50000"
|
||||
},
|
||||
{
|
||||
"Name": "rss-monitor",
|
||||
"Timeout": "5s",
|
||||
"IP": "localhost",
|
||||
"Port": "28686",
|
||||
"Log": "./alt5.log",
|
||||
"DB": "./alt5.db",
|
||||
"Measure": false,
|
||||
"Intervalunit": "1s",
|
||||
"ConfFile": "./conf.json",
|
||||
"Overwrite": true
|
||||
},
|
||||
{
|
||||
"Name": "sev-alerts",
|
||||
"IP": "https://qualtrics.atlassian.net/sr/jira.issueviews:searchrequest-rss/44938/SearchRequest-44938.xml?tempMax=1000",
|
||||
"Port": "YnJlZWxAcXVhbHRyaWNzLmNvbTpeKllycGl6Y3cmNTRPd3Yw",
|
||||
"Timeout": "720h",
|
||||
"Intervalunit": "30m"
|
||||
},
|
||||
{
|
||||
"Name": "email",
|
||||
"Log": "breellocaldev@gmail.com",
|
||||
"IP": "smtp.gmail.com:465",
|
||||
"IP2": "pop.gmail.com:995",
|
||||
"Port": "ML3WQRFSqe9rQ8qNkm",
|
||||
"DB": "mailto:3852010864@txt.att.net",
|
||||
"Pub": "xsBNBFsn3DkBCADFA7dD+urJE82QrqsTK9cPS4S1stfCglnh4kpFQRO98cX2FRYlIlWJYnVr8IUTI+fAb0XymB+kLOUYtgMQp7vK7MbbowG6sUBc7Eb4TNOOYm+LuUq4ECDvWJIuT4EWt4aeY9g7IR0Llbm5ec7pe8hfABVUABrz8ZnFOufD5Gkd0/O3aATaKmZJ3K3aPQO7jnfVOzn4US5lo0C+Dt1B/HFEKOeRnaeOPqRNm1cn9Vq+Vw+9l9E8SPU1mGZbELMaY19A/BRGTOhliWosTKzcG9u8QG3eNb1/jG9xduS0QWaCEHjYgPqYvNJ1pHcKEdrGZj69RLWgQOkc2mitAR+gkzYLABEBAAHNAMLAYgQTAQgAFgUCWyfcOQkQVnaLXPzpsa4CGwMCGQEAAFj2CAAtVKdj+SPvG1Rd7bXXv6i3mjEyfwkV1XSxda8nVkx72MK6utYXATLM5BNVkCCeK7CTaOnpNwfUZ+TqPlmr2G+cWGfPEDApBS1ajuK65sKeppZ7tQEocZLKCdUcqqLaPycOVNW1ZoNqrCurHQPJdWEJAtplYFDKOptMfcwFX3AkR27eYV286b1Kk+LM26h+SAJUEZ78Ixf6vkp5nfTle1wBANhASZ7XhUWyxWrJ7gNW/1N82ThlkHr+FxrhMfGyIoihtpaFdMnh3RLgnNs3pfnMGNOdTVo1DVh294EQagG+CW1Lt68LekHSxUbFvXVwUHdufLTO8+6SI3iSquuR6NR1zsBNBFsn3DkBCAC8Kg61Gu/lu91QM/Ze/bzAP3IqNIaQjjgB23UEM1XoHgyMcDCTnuwx0IFjak/a7LxEBiaUWC6oaRqdwuPa5lNr2KrpoBsh4hUBwySRTRQrHymwYFBI8qBYfdoO8jP7C70RoiOCXX9j5I/V0g3gneFxbIOiixrILDIZj5jlSkMpSGG0vDOsC8GpqWRMOs4/F5zSN0UKzq4Vr67zizcDnfD9/9qrfAxOw0GOohl97ePxfgFoVZckkRgAOJylGgGQ3ihwt7MBf8LOL3shY0wMYqvxSc2UcJPbPH3kfKfMi+XhAlwCAdnlRdKEOCn1svhAvcxv5FDVnTiS7I1HGH7AfCP5ABEBAAHCwF8EGAEIABMFAlsn3DkJEFZ2i1z86bGuAhsMAACxFwgAFR3jG0f+tqgGxzgGsSDhcZC9h1QfWvh1nw/q0EZzwI8+lAsEfBeVNOjZttZI+RMyO9NBTwJErq4EeuemNCqu93zJOf3i45G0+jGV9/XMga5641CfIuFPKkcWBMR67iiEu2/vG7e9l1IkR74pBJhp2ub8aaivWvNS1lloYESee0euGu/TOXe7FO6V6E1KVw+17A78NlG09WCO5c5SZdDI9YCXxEeqHzNH4j2RrAxNsYZ3M6L0+JbVqwWFN73OUi6EM7fQFdpXCryKV6OEKnYKeGPW2TRUrT+gBgLnJV0crVTibaOSXdq+vyuHszKdT0lcLmEj2F3i73vmvUKYMh/shA=="
|
||||
}
|
||||
]
|
||||
166
vendor/local/system/sysconf/const.go
vendored
Executable file
166
vendor/local/system/sysconf/const.go
vendored
Executable file
@@ -0,0 +1,166 @@
|
||||
package sysconf
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"local/logger"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Conf struct {
|
||||
Name string
|
||||
Pub string
|
||||
Port string
|
||||
IP string
|
||||
IP2 string
|
||||
TimeOut time.Duration
|
||||
Timeout string
|
||||
Log string
|
||||
DB string
|
||||
Measure bool
|
||||
IntervalUnit time.Duration
|
||||
Intervalunit string
|
||||
Overwrite bool
|
||||
ConfFile string
|
||||
}
|
||||
|
||||
var configs map[string]Conf
|
||||
|
||||
func (c Conf) String() string {
|
||||
kv := make(map[string]interface{})
|
||||
kv["name"] = c.Name
|
||||
kv["port"] = c.Port
|
||||
kv["IP"] = c.IP
|
||||
kv["IP2"] = c.IP2
|
||||
out := ""
|
||||
for k, v := range kv {
|
||||
out += fmt.Sprintf("%v:%v ", k, v)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func Get(name string) Conf {
|
||||
if configs == nil {
|
||||
open()
|
||||
}
|
||||
if _, ok := configs[name]; !ok {
|
||||
panic("Unknown service " + name)
|
||||
}
|
||||
logger.Log("config: got", name, "config: ", configs[name])
|
||||
return configs[name]
|
||||
}
|
||||
|
||||
func open() {
|
||||
f, err := ioutil.ReadFile(path.Join(os.Getenv("GOPATH"), "src", "local1", "system", "sysconf", "conf.json"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var jsonConfs []Conf
|
||||
err = json.Unmarshal([]byte(string(f)), &jsonConfs)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
configs = make(map[string]Conf)
|
||||
ports := make(map[string]bool)
|
||||
initPort := 28685
|
||||
thisPort := 0
|
||||
for i := range jsonConfs {
|
||||
portstr := jsonConfs[i].Port
|
||||
portlist := strings.Split(portstr, ",")
|
||||
for j := range portlist {
|
||||
ports[portlist[j]] = true
|
||||
}
|
||||
}
|
||||
for i := range jsonConfs {
|
||||
if jsonConfs[i].Timeout != "" {
|
||||
jsonConfs[i].TimeOut, err = time.ParseDuration(jsonConfs[i].Timeout)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
if jsonConfs[i].Intervalunit != "" {
|
||||
jsonConfs[i].IntervalUnit, err = time.ParseDuration(jsonConfs[i].Intervalunit)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
if jsonConfs[i].Port == "" {
|
||||
thisPort += 1
|
||||
port := strconv.Itoa(initPort + thisPort)
|
||||
for _, ok := ports[port]; ok; _, ok = ports[port] {
|
||||
port = strconv.Itoa(initPort + thisPort)
|
||||
thisPort++
|
||||
}
|
||||
jsonConfs[i].Port = port
|
||||
ports[jsonConfs[i].Port] = true
|
||||
}
|
||||
if jsonConfs[i].Log != "" && jsonConfs[i].Log[0] == '.' {
|
||||
jsonConfs[i].Log = os.Getenv("MNT") + jsonConfs[i].Log
|
||||
}
|
||||
if jsonConfs[i].DB != "" && jsonConfs[i].DB[0] == '.' {
|
||||
jsonConfs[i].DB = os.Getenv("MNT") + jsonConfs[i].DB
|
||||
}
|
||||
if jsonConfs[i].ConfFile != "" && jsonConfs[i].ConfFile[0] == '.' {
|
||||
jsonConfs[i].ConfFile = os.Getenv("MNT") + jsonConfs[i].ConfFile
|
||||
}
|
||||
|
||||
configs[jsonConfs[i].Name] = jsonConfs[i]
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
var (
|
||||
Broadcaster = Conf{
|
||||
Name: "broadcaster",
|
||||
Port: "28688",
|
||||
Timeout: time.Second * 5,
|
||||
IP: "localhost",
|
||||
}
|
||||
JBTServe = Conf{
|
||||
Name: "jbt-serve",
|
||||
Port: "28080",
|
||||
Timeout: time.Second * 5,
|
||||
IP: "localhost",
|
||||
}
|
||||
JBTClient = Conf{
|
||||
Name: "jbt-client",
|
||||
Port: "28081",
|
||||
Timeout: time.Second * 5,
|
||||
IP: "localhost",
|
||||
}
|
||||
NATServe = Conf{
|
||||
Name: "nat-serve",
|
||||
Port: "28447",
|
||||
Timeout: time.Second * 5,
|
||||
IP: "localhost",
|
||||
}
|
||||
RSSMon = Conf{
|
||||
Name: "rss-monitor",
|
||||
Port: "28686",
|
||||
Timeout: time.Second * 5,
|
||||
IP: "localhost",
|
||||
Log: "./my.log",
|
||||
DB: "./my.db",
|
||||
Measure: false,
|
||||
IntervalUnit: time.Second,
|
||||
}
|
||||
SevAlerts = Conf{
|
||||
Name: "sev-alerts",
|
||||
IP: "https://qualtrics.atlassian.net/sr/jira.issueviews:searchrequest-rss/44938/SearchRequest-44938.xml?tempMax=1000",
|
||||
Port: "YnJlZWxAcXVhbHRyaWNzLmNvbTpeKllycGl6Y3cmNTRPd3Yw",
|
||||
Timeout: time.Minute * 60,
|
||||
IntervalUnit: time.Minute * 15,
|
||||
}
|
||||
Email = Conf{
|
||||
Name: "bbarl64@gmail.com",
|
||||
IP: "smtp.gmail.com:465",
|
||||
Port: ">_w2Xeq\"tvJkv5y\\",
|
||||
DB: "3852010864@txt.att.net",
|
||||
}
|
||||
)
|
||||
*/
|
||||
5
vendor/local/system/sysconf/topic.go
vendored
Executable file
5
vendor/local/system/sysconf/topic.go
vendored
Executable file
@@ -0,0 +1,5 @@
|
||||
package sysconf
|
||||
|
||||
var (
|
||||
RSSFeedItem = "rssFeedItem."
|
||||
)
|
||||
Reference in New Issue
Block a user