Skip to content

Commit

Permalink
dns
Browse files Browse the repository at this point in the history
  • Loading branch information
vlorc committed Dec 12, 2018
1 parent 27dca67 commit d81d463
Show file tree
Hide file tree
Showing 18 changed files with 350 additions and 73 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func main() {
## Lua Demo
+ [tcp](https://github.com/vlorc/lua-vm/blob/master/demo/tcp.lua)
+ [udp](https://github.com/vlorc/lua-vm/blob/master/demo/udp.lua)
+ [dns](https://github.com/vlorc/lua-vm/blob/master/demo/dns.lua)
+ [http](https://github.com/vlorc/lua-vm/blob/master/demo/http.lua)
+ [time](https://github.com/vlorc/lua-vm/blob/master/demo/time.lua)
+ [hash](https://github.com/vlorc/lua-vm/blob/master/demo/hash.lua)
Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func main() {
## Lua 例子
+ [tcp](https://github.com/vlorc/lua-vm/blob/master/demo/tcp.lua)
+ [udp](https://github.com/vlorc/lua-vm/blob/master/demo/udp.lua)
+ [dns](https://github.com/vlorc/lua-vm/blob/master/demo/dns.lua)
+ [http](https://github.com/vlorc/lua-vm/blob/master/demo/http.lua)
+ [time](https://github.com/vlorc/lua-vm/blob/master/demo/time.lua)
+ [hash](https://github.com/vlorc/lua-vm/blob/master/demo/hash.lua)
Expand Down
54 changes: 31 additions & 23 deletions base/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func (BufferFactory) Form(str string) Buffer {
return Buffer(str)
}

func (BufferFactory) FormInt(val ...int) Buffer {
return __intBuffer(val...)
func (BufferFactory) FormNumber(val ...int) Buffer {
return __numberBuffer(val...)
}

func (BufferFactory) FormString(val ...string) Buffer {
Expand Down Expand Up @@ -79,27 +79,19 @@ func (b Buffer) Clone(args ...int) Buffer {
return dst
}

func (b Buffer) Copy(src Buffer, args ...int) {
if len(src) > 0 {
copy(b.Slice(args...), src)
}
func (b Buffer) Copy(src Buffer, args ...int) int {
return copy(b.Slice(args...), src)
}

func (b Buffer) Concat(src ...Buffer) Buffer {
i := 0
i := len(b)
for _, v := range src {
i += len(v)
}
dst := make(Buffer, i+len(b))
i = len(b)
if i > 0 {
copy(dst, b)
}
dst := make(Buffer, i)
i = copy(dst, b)
for _, v := range src {
if len(v) > 0 {
copy(dst[i:], v)
i += len(v)
}
i += copy(dst[i:], v)
}
return dst
}
Expand All @@ -109,7 +101,7 @@ func (b Buffer) Equal(buf Buffer) bool {
}

func (b Buffer) String() string {
return __hexUpperString(b)
return __HEXString(b)
}

func (b Buffer) ToString(args ...string) string {
Expand All @@ -124,16 +116,22 @@ func (b Buffer) ToString(args ...string) string {

func (b Buffer) ToRune(args ...int) rune {
n := b.Slice(args...)
v, _ := utf8.DecodeRune(n)
return v
r, _ := utf8.DecodeRune(n)
return r
}

func (b Buffer) ToNumber(args ...int) (v int64) {
func (b Buffer) ToNumber(args ...int) (r uint64) {
n := b.Slice(args...)
for i := len(n) - 1; i >= 0; i-- {
v = (v << 8) + int64(n[i])
if 3 != len(args) {
for i := len(n) - 1; i >= 0; i-- {
r = (r << 8) + uint64(n[i])
}
} else {
for _, v := range n {
r = (r << 8) + uint64(v)
}
}
return v
return r
}

func (b Buffer) ToLine(args ...int) string {
Expand All @@ -147,3 +145,13 @@ func (b Buffer) ToLine(args ...int) string {
}
return __rawString(n)
}

func (b Buffer) ToHash(args ...string) uint64 {
if len(args) <= 0 {
return __hashChecksum8(b)
}
if hash, ok := HashTable[args[0]]; ok {
return hash(b)
}
return 0
}
54 changes: 47 additions & 7 deletions base/encode.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package base

import (
"crypto/md5"
"crypto/sha1"
"encoding/base32"
"encoding/base64"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
Expand All @@ -10,22 +13,27 @@ import (
var EncodeTable = map[string]func(Buffer) string{
"raw": __rawString,
"utf8": __rawString,
"HEX": __hexUpperString,
"hex": __hexLowerString,
"HEX": __HEXString,
"hex": __hexString,
"base32": __base32String,
"base64": __base64String,
"gbk": __gbkString,
"gb18030": __gb18030String,
"md5": __md5String,
"MD5": __MD5String,
"sha1": __sha1String,
"SHA1": __SHA1String,
}

func __hexUpperString(src Buffer) string {
return __hexString(src, "0123456789ABCDEF")
func __HEXString(src Buffer) string {
return __hex(src, "0123456789ABCDEF")
}

func __hexLowerString(src Buffer) string {
return __hexString(src, "0123456789abcdef")
func __hexString(src Buffer) string {
return __hex(src, "0123456789abcdef")
}

func __hexString(src Buffer, tab string) string {
func __hex(src Buffer, tab string) string {
dst := make([]byte, len(src)*2)
for i, v := range src {
dst[i*2+0] = tab[v>>4]
Expand All @@ -34,6 +42,10 @@ func __hexString(src Buffer, tab string) string {
return *(*string)(unsafe.Pointer(&dst))
}

func __base32String(src Buffer) string {
return base32.StdEncoding.EncodeToString(src)
}

func __base64String(src Buffer) string {
return base64.StdEncoding.EncodeToString(src)
}
Expand All @@ -51,3 +63,31 @@ func __gbkString(src Buffer) string {
dst, _, _ := transform.Bytes(simplifiedchinese.GBK.NewDecoder(), src)
return *(*string)(unsafe.Pointer(&dst))
}

func __md5String(src Buffer) string {
h := md5.New()
h.Write(src)
dst := h.Sum(nil)
return __hexString(dst)
}

func __MD5String(src Buffer) string {
h := md5.New()
h.Write(src)
dst := h.Sum(nil)
return __HEXString(dst)
}

func __sha1String(src Buffer) string {
h := sha1.New()
h.Write(src)
dst := h.Sum(nil)
return __hexString(dst)
}

func __SHA1String(src Buffer) string {
h := sha1.New()
h.Write(src)
dst := h.Sum(nil)
return __HEXString(dst)
}
67 changes: 67 additions & 0 deletions base/hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package base

import (
"hash/crc32"
"hash/crc64"
)

var HashTable = map[string]func(Buffer) uint64{
"checksum8": __hashChecksum8,
"checksum16": __hashChecksum16,
"checksum32": __hashChecksum32,
"checksum64": __hashChecksum64,
"crc32": __hashCrc32,
"crc64": __hashCrc64ISO,
"crc64.iso": __hashCrc64ISO,
"crc64.ecma": __hashCrc64ECMA,
}

func __hashChecksum8(b Buffer) uint64 {
r := byte(0)
for _, v := range b {
r += v
}
return uint64(r)
}

func __hashChecksum16(b Buffer) uint64 {
r := uint16(0)
for _, v := range b {
r += uint16(v)
}
return uint64(r)
}

func __hashChecksum32(b Buffer) uint64 {
r := uint32(0)
for _, v := range b {
r += uint32(v)
}
return uint64(r)
}

func __hashChecksum64(b Buffer) uint64 {
r := uint64(0)
for _, v := range b {
r += uint64(v)
}
return uint64(r)
}

func __hashCrc32(b Buffer) uint64 {
h := crc32.NewIEEE()
h.Write(b)
return uint64(h.Sum32())
}

func __hashCrc64ISO(b Buffer) uint64 {
h := crc64.New(crc64.MakeTable(crc64.ISO))
h.Write(b)
return h.Sum64()
}

func __hashCrc64ECMA(b Buffer) uint64 {
h := crc64.New(crc64.MakeTable(crc64.ECMA))
h.Write(b)
return h.Sum64()
}
28 changes: 27 additions & 1 deletion base/tobuffer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package base

import (
"encoding"
"encoding/json"
"github.com/yuin/gopher-lua"
"layeh.com/gopher-luar"
"reflect"
Expand Down Expand Up @@ -36,8 +38,32 @@ func __newBuffer(v lua.LValue) Buffer {
func __dataBuffer(v interface{}) Buffer {
reflect.TypeOf(v)
switch r := v.(type) {
case *[]byte:
if nil != r {
return Buffer(*r)
}
case *Buffer:
if nil != r {
return Buffer(*r)
}
case *string:
if nil != r {
return Buffer(*r)
}
case []byte:
return Buffer(r)
case encoding.BinaryMarshaler:
if b, err := r.MarshalBinary(); nil != err && len(b) > 0 {
return Buffer(b)
}
case encoding.TextMarshaler:
if b, err := r.MarshalText(); nil != err && len(b) > 0 {
return Buffer(b)
}
case json.Marshaler:
if b, err := r.MarshalJSON(); nil != err && len(b) > 0 {
return Buffer(b)
}
}
return nil
}
Expand Down Expand Up @@ -69,7 +95,7 @@ func __stringBuffer(val ...string) Buffer {
return buf
}

func __intBuffer(val ...int) Buffer {
func __numberBuffer(val ...int) Buffer {
buf := make(Buffer, len(val))
for i, v := range val {
buf[i] = byte(v)
Expand Down
17 changes: 17 additions & 0 deletions crypto/hash/hash.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hash

import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
Expand All @@ -13,6 +14,7 @@ type SHA1Factory struct{}
type SHA256Factory struct{}
type SHA512Factory struct{}
type MD5Factory struct{}
type HMACFactory struct{}

func __sum(h hash.Hash, buf ...base.Buffer) base.Buffer {
for _, v := range buf {
Expand Down Expand Up @@ -52,3 +54,18 @@ func (SHA512Factory) New() hash.Hash {
func (SHA512Factory) Sum(buf ...base.Buffer) base.Buffer {
return __sum(sha512.New(), buf...)
}

var __table = map[string]func() hash.Hash{
"md5": md5.New,
"sha1": sha1.New,
"sha256": sha256.New,
"sha512": sha512.New,
}

func (HMACFactory) New(method, secret string) hash.Hash {
return hmac.New(__table[method], []byte(secret))
}

func (f HMACFactory) Sum(method, secret string, buf ...base.Buffer) base.Buffer {
return __sum(f.New(method, secret), buf...)
}
File renamed without changes.
6 changes: 6 additions & 0 deletions demo/dns.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local dns = require("net.dns")
local ips = dns:lookup("www.baidu.com")

for key,value in ips() do
print(key, value)
end
8 changes: 4 additions & 4 deletions fs/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (NativeFileFactory) Open(file string, args ...int) (FileDriver, error) {
}

func (NativeFileFactory) Rename(src, dst string) error {
return os.Rename(src,dst)
return os.Rename(src, dst)
}

func (NativeFileFactory) Remove(file string) error {
Expand All @@ -43,10 +43,10 @@ func (NativeFileFactory) Exist(file string) bool {
return true
}

func (NativeFileFactory) Mkdir(file string,mode int) error {
return os.MkdirAll(file,os.FileMode(mode))
func (NativeFileFactory) Mkdir(file string, mode int) error {
return os.MkdirAll(file, os.FileMode(mode))
}

func (NativeFileFactory) Walk(root string,callback filepath.WalkFunc) error {
func (NativeFileFactory) Walk(root string, callback filepath.WalkFunc) error {
return filepath.Walk(root, callback)
}
Loading

0 comments on commit d81d463

Please sign in to comment.