summaryrefslogtreecommitdiff
AgeCommit message (Collapse)Author
2021-12-03Release share v0.32.0 (2021-12-03)v0.32.0Shulhan
This release bring major changes and enhancements to package dns and math/big. === Breaking changes * math/big: refactoring AddFloat to accept variadic interface{} Previously, the AddFloat function only accept two interface parameters. This changes make the AddFloat to accept many interface parameters. * math/big: refactor NewFloat to accept interface{} instead of float64 only The following types is added and can be initialized to Float: []byte, uint, uint16, uint32, uint64, big.Int, *big.Int, big.Rat, and *big.Rat. * math/big: refactoring the Rat The promise of this package is to provide the Rat type with RoundToZero and without panic. In order to do that we remove the MustRat() function and check for nil on pointer receiver before doing operation, and check for zero value when doing quotation. Another breaking changes are, * Humanize() with nil pointer receiver will return string "0" * Any IsXxx() methods with nil pointer receiver will return false * MarshalJSON() will nil pointer receiver will return "null" instead of "0" now * lib/dns: refactoring, change signature of client Lookup Previously, Lookup() method on Client pass the question name, type, and class as parameters. This changes make those parameters into MessageQuestion. * lib/dns: refactoring ZoneFile into Zone Reason: A Zone is not always represented by file, it just that in this package, it is. This changes rename the type ZoneFile into Zone. * lib/dns: refactoring message question Previously the type for message question section SectionQuestion. This changes, rename the type to MessageQuestion. * lib/dns: refactoring the section header This changes rename the SectionHeader into MessageHeader. The pack() method is optimized with the following results, benchmark old ns/op new ns/op delta BenchmarkMessageHeader_pack-8 66.2 21.7 -67.31% benchmark old allocs new allocs delta BenchmarkMessageHeader_pack-8 3 1 -66.67% benchmark old bytes new bytes delta BenchmarkMessageHeader_pack-8 32 16 -50.00% The unpack() method is simplified by minimizing the if-condition. This changes also fix the pack and unpack OpCode for value other then 0, due to wrong shift value, 2 instead of 3. * lib/dns: refactoring, introduce new type RecordClass Previously, the record class is represented by uint16 using prefix QueryClassXxx. This changes make the record class to be an independent type, to make code more strict (prevent passing invalid value), and readable. * lib/dns: refactoring, create type RecordType to represent type of RR Previously, we use uint16 to represent type for ResourceRecord Type or Question type. To make the code more strict, where parameter or return value, must be expected as record type, we add new type to represent the RR type: RecordType. This changes also rename any variable name of QType or qtype to RType or rtype because QType is misleading. The type defined the ResourceRecord to be queried not only question. * lib/dns: remove the fallback name servers (NS) from server options The original idea of fallback NS is to send the query to the one define to original resolv.conf, instead of using the one defined by user in ServerOptions NameServers, when an error occured. But, most of error usually caused by network (disconnected, time out), so re-sending query to fallback NS does not have any effect if the network it self is not working. This changes remove the unnecessary and complex fallback NS from server. === New features * lib/dns: add support to save and load caches to/from storage The CachesSave method write the non-local answers into an io.Writer, encoded with gob. The CachesLoad method load the gob encoded answers from an io.Reader. * lib/ssh: use agent defined by config or from environment variable Previously, we only check the environment variable SSH_AUTH_SOCK to decide whether the client identity will fetched from agent or from private key file. This changes use the GetIdentityAgent from config Section to derive the path to unix socket. It will return an empty string if the IdentityAgent is set to "none" in the Section or when SSH_AUTH_SOCK is not exist or empty. * ssh/config: add support for section variable IdentityAgent There are four possible value for IdentityAgent: SSH_AUTH_SOCK, <$STRING>, <PATH>, or "none". If SSH_AUTH_SOCK, the socket path is read from the environment variable SSH_AUTH_SOCK. If value start with "$", then the socket path is set based on value of that environment variable. Other string beside "none" will be considered as path to socket. === Bug fixes * math/big: return nil on Quo and QuoRat instead of panic Previously, if the first parameter of Quo or the second/next parameters of QuoRat is not convertable to Rat or zero, the method/function will panic. This changes make it less intrusive, instead of panic we check for zero value and return nil immediately. * lib/dns: do not cache truncated answer Previously only answer with non-zero response code is ignored. This changes ignore also answer where response header is truncated. === Enhancements * lib/dns: realign all struct fields Turns out the struct that we frequently used, answer and resource_record, is not optimized. answer.go:15:13: struct with 56 pointer bytes could be 24 = 32 bytes ... resource_record.go:24:21: struct with 56 pointer bytes could be 40 = 16 This changes reorder all structs field to save space in memory. * lib/dns: make the TCP forwarders as complementary of UDP The TCP forwarders only active when client send the DNS request as TCP. When the server receive that request it should also forward the request as TCP not as UDP to prevent the truncated response. Another use case for TCP is when the response is truncated, the client will send the query back through TCP connection. The server should forward this request using TCP instead of UDP. * lib/dns: use different packet between UDP and TCP messages Previously, all packet size for reading and sending the message is fixed to 4096, even on UDP. This changes set the UDP packet size maximum to 512 bytes and others to 4096 bytes. While at it, minimize copying packet if its not reusable inside a method.
2021-12-03math/big: refactoring AddFloat to accept variadic interface{}Shulhan
Previously, the AddFloat function only accept two interface parameters. This changes make the AddFloat to accept many interface parameters.
2021-12-03math/big: refactor NewFloat to accept interface{} instead of float64 onlyShulhan
The following types is added and can be initialized to Float: []byte, uint, uint16, uint32, uint64, big.Int, *big.Int, big.Rat, and *big.Rat.
2021-11-19math/big: realign struct fields on unit testsShulhan
The following alignment reported and fixed by fieldaligment tool, float_test.go:103:13: struct with 16 pointer bytes could be 8 float_test.go:254:13: struct with 16 pointer bytes could be 8 big/float_test.go:302:13: struct with 32 pointer bytes could be 16
2021-11-19math/big: refactoring the RatShulhan
The promise of this package is to provide the Rat type with RoundToZero and without panic. In order to do that we remove the MustRat() function and check for nil on pointer receiver before doing operation, and check for zero value when doing quotation. Another breaking changes are, * Humanize() with nil pointer receiver will return string "0" * Any IsXxx() methods with nil pointer receiver will return false * MarshalJSON() will nil pointer receiver will return "null" instead of "0" now
2021-11-17math/big: return nil on Quo and QuoRat instead of panicShulhan
Previously, if the first parameter of Quo or the second/next parameters of QuoRat is not convertable to Rat or zero, the method/function will panic. This changes make it less intrunsive, instead of panic we check for zero value and return nil immediately.
2021-11-16lib/dns: refactoring, change signature of client LookupShulhan
Previously, Lookup() method on Client pass the question name, type, and class as parameters. This changes make those parameters into MessageQuestion.
2021-11-16lib/dns: check error on Decode() in caches.readShulhan
2021-11-16lib/dns: remove ineffectual assignment to err in Server runTCPForwarderShulhan
2021-11-15lib/dns: refactoring ZoneFile into ZoneShulhan
Reason: A Zone is not always represented by file, it just that in this package, it is. This changes rename the type ZoneFile into Zone.
2021-11-15lib/{email,spf}: changes related to refactoring lib/dnsShulhan
2021-11-15lib/dns: realign all struct fieldsShulhan
Using the fieldalignment, we got the following output, answer.go:15:13: struct with 56 pointer bytes could be 24 = 32 bytes caches.go:27:13: struct with 24 pointer bytes could be 16 = 8 dot_client.go:20:16: struct with 16 pointer bytes could be 8 = 8 hosts_file.go:24:16: struct with 64 pointer bytes could be 48 = 16 message.go:53:14: struct of size 176 could be 168 = 8 rdata_mx.go:20:14: struct with 16 pointer bytes could be 8 = 8 rdata_opt.go:18:15: struct with 16 pointer bytes could be 8 = 8 rdata_srv.go:16:15: struct with 64 pointer bytes could be 56 = 8 rdata_wks.go:30:15: struct with 40 pointer bytes could be 32 = 8 request.go:20:14: struct with 32 pointer bytes could be 24 = 8 resource_record.go:24:21: struct with 56 pointer bytes could be 40 = 16 server.go:76:13: struct with 120 pointer bytes could be 112 = 8 server_options.go:20:20: struct of size 240 could be 224 = 16 tcp_client.go:21:16: struct with 40 pointer bytes could be 24 = 16 udp_client.go:24:16: struct with 24 pointer bytes could be 16 = 8 udp_client_pool.go:25:20: struct with 24 pointer bytes could be 16 = 8 zone_file.go:22:15: struct with 128 pointer bytes could be 120 = 8 zone_parser.go:53:17: struct with 88 pointer bytes could be 72 = 16 Turns out the struct that we frequently used, answer and resource_record, is not optimized. This changes reorder all structs field to save space in memory.
2021-11-15lib/dns: refactoring message questionShulhan
Previously the type for message question section SectionQuestion. This changes, rename the type to MessageQuestion.
2021-11-14lib/dns: refactoring the section headerShulhan
This changes rename the SectionHeader into MessageHeader. The pack() method is optimized with the following results, benchmark old ns/op new ns/op delta BenchmarkMessageHeader_pack-8 66.2 21.7 -67.31% benchmark old allocs new allocs delta BenchmarkMessageHeader_pack-8 3 1 -66.67% benchmark old bytes new bytes delta BenchmarkMessageHeader_pack-8 32 16 -50.00% The unpack() method is simplified by minimizing the if-condition. This changes also fix the pack and unpack OpCode for value other then 0, due to wrong shift value, 2 instead of 3.
2021-11-14lib/dns: refactoring, introduce new type RecordClassShulhan
Previously, the record class is represented by uint16 using prefix QueryClassXxx. This changes make the record class to be an independent type, to make code more strict (prevent passing invalid value), and readable.
2021-11-14lib/dns: refactoring, create type RecordType to represent type of RRShulhan
Previously, we use uint16 to represent type for ResourceRecord Type or Question type. To make the code more strict, where parameter or return value, must be expected as record type, we add new type to represent the RR type: RecordType. This changes also rename any variable name of QType or qtype to RType or rtype because QType is misleading. The type defined the ResourceRecord to be queried not only question.
2021-11-14lib/dns: add init method to RDataSOAShulhan
The ini method initialize the SOA record by setting fields to its default value if its empty. The default value for RName is "root", for Serial is current epoch, for Refresh is 1 day, for Retry is 1 hour, and for Minimum is 1 hour. While at it, update the comments to be more understandable and represent the type or fields.
2021-11-13lib/dns: remove internal field off from ResourceRecordShulhan
The off field previously used to record the next index after parsing domain name with offset. Since the field only used once, after calling unpackDomainName, we return the off value instead as "end". This changes make the unpackDomainName() become a function. This changes rename field offTTL to idxTTL to match with their value. This changes alos pefix all errors with the method or function names.
2021-11-13lib/dns: remove unnecessary string convertionShulhan
2021-11-13lib/dns: simplify and add unit testing on zoneRecordsShulhan
2021-11-13lib/dns: make the TCP forwarders as complementary of UDPShulhan
The TCP forwarders only active when client send the DNS request as TCP. When the server receive that request it should also forward the request as TCP not as UDP to prevent the truncated response. Another use case for TCP is when the response is truncated, the client will send the query back through TCP connection. The server should forward this request using TCP instead of UDP.
2021-11-13lib/dns: remove the fallback name servers (NS) from server optionsShulhan
The original idea of fallback NS is to send the query to the one define to original resolv.conf, instead of using the one defined by user in ServerOptions NameServers, when an error occured. But, most of error usually caused by network (disconnected, time out), so re-sending query to fallback NS does not have any effect if the network it self is not working. This changes remove the unnecessary and complex fallback NS from server.
2021-11-13lib/dns: use snake case for file namingShulhan
Using snake case make the file naming more readable.
2021-11-12lib/dns: log the question when forward failedShulhan
2021-11-11lib/dns: simplify parse name servers on server optionsShulhan
Instead of returning four variables, the parseNameServers now have parameter isPrimary to set the primary address or fallback address.
2021-11-11lib/dns: do not cache truncated answerShulhan
Previously only answer with non-zero response code is ignored. This changes ignore also answer where response header is truncated.
2021-11-11lib/dns: use different packet between UDP and TCP messagesShulhan
Previously, all packet size for reading and sending the message is fixed to 4096, even on UDP. This changes set the UDP packet size maximum to 512 bytes and others to 4096 bytes. While at it, minimize copying packet if its not reusable inside a method.
2021-11-08lib/dns: add support to save and load caches to/from storageShulhan
The CachesSave method write the non-local answers into an io.Writer, encoded with gob. The CachesLoad method load the gob encoded answers from an io.Reader.
2021-11-06lib/ssh: use agent defined by config or from environment variableShulhan
Previously, we only check the environment variable SSH_AUTH_SOCK to decide whether the client identity will fetched from agent or from private key file. This changes use the GetIdentityAgent from config Section to derive the path to unix socket. It will return an empty string if the IdentityAgent is set to "none" in the Section or when SSH_AUTH_SOCK is not exist or empty.
2021-11-06ssh/config: add support for section variable IdentityAgentShulhan
There are four possible value for IdentityAgent: SSH_AUTH_SOCK, <$STRING>, <PATH>, or "none". If SSH_AUTH_SOCK, the socket path is read from the environment variable SSH_AUTH_SOCK. If value start with "$", then the socket path is set based on value of that environment variable. Other string beside "none" will be considered as path to socket.
2021-11-04Release share v0.31.0 (2021-11-04)v0.31.0Shulhan
=== Breaking changes * lib/memfs: move the embedded parameter to Options Since the GoEmbed can be called only when MemFS instance is initiated, it would be better if parameters for GoEmbed also initialized in the struct Options. In this way any additional parameters needed to add to GoEmbed does not changes the method signature in the future. This commit add new type EmbedOptions that contains the parameters for GoEmbed. In this new type, we add new field EmbedWithoutModTime. if its true, the modification time for all files and directories are not stored inside generated code, instead all files will use the current time when the program is running. * lib/totp: make the New to accept only hash with SHA1, SHA256, or SHA512 Previously, the first parameter to New is a function that return hash.Hash. This signature can be misleading, because md5.New also can return hash.Hash but not usable in TOTP. This changes update the New function signature to accept defined type that can be set to SHA1, SHA256, or SHA512. * lib/bytes: refactoring and cleaning up the bytes package The bytes package, and many other packages in this module, is written when I still learning and using the Go language for master thesis. Some of the code, like function signature, does not follow the Go idiom, at least not how the Go source code looks like. A breaking changes, * WriteUint16 and WriteUint32 accept slice only without pointer. There is no need to pass slice as pointer to function if we want to modify the content, as long as the backing storage is not changed. Bug fixes, * PrintHex: fix print layout on the last line * ReadHexByte: fix possible index out of range * SkipAfterToken return -1 and false if no token found, as promised in the comment, instead of the length of text. We move all unit test to example so we have test and example in the documentation at the same time. This changes make all test coverage 100%. * lib/bytes: refactoring AppendXxx functions Previously, we pass pointer to slice on AppendInt16, AppendInt32, AppendUint16, and AppendUint32 functions. This model of function signature is not a Go idiom. It is written when I am still new to Go. * lib/ascii: change signature of ToLower and ToUpper Using pointer to slice on method or function is not a Go idiom. It is created when I still new to Go. * lib/memfs: refactoring Node field V into Content The reason why the field named V is because it's short. It's come from my C/C++ experience that got carried away when writing this package. Now, after having more time writing Go, I prefer clarity over cleverity(?). * lib/memfs: set the node modification time in embedded file This changes set all node modification time in embedded files to the node modTime using Unix() and Nanosecond() values. Since the time will always changes we need to remove the test to generate file gen_test.go to prevent the file being modified and re-adding the same file every time we run local tests. === New features * lib/ini: add function IsValidVarName The IsValidVarName check if "v" is valid variable name, where the first character must be a letter and the rest should contains only letter, digit, period, hyphen, or underscore. If "v" is valid it will return true. * lib/memfs: set the node modification time in embedded file This changes set all node modification time in embedded files to the node modTime using Unix() and Nanosecond() values. Since the time will always changes we need to remove the test to generate file gen_test.go to prevent the file being modified and re-adding the same file every time we run local tests. * lib/io: add method String to FileState type The String method return the string representation of FileState. Usually used only for debugging. * lib/smtp: implement method Noop on Client Noop send the NOOP command to server with optional message. On success, it will return response with Code 250, StatusOK. While at it fix double call to recv on Reset() method. * lib/smtp: implement method Reset on Client The Reset() method send the STMP RSET command to the server. This command clear the current buffer on MAIL, RCPT, and DATA, but not the EHLO/HELO buffer. On success, it will return response with Code 250, StatusOK. === Bug fixes * lib/ascii: fix IsHex return false on 0 * lib/memfs: fix parent empty directory not removed Use case: x x/y If x/y is empty, and x processed first, the x will not be removed. This commit fix this, by sorting the paths in descending order first to make empty parent removed clearly. In above case the order or check become, x/y x While at it, update an example of New to give preview of input and what the expected output for certain operations. * lib/xmlrpc: rewrite the Client connection using lib/http Using socket connection require reading the HTTP response header before we can process the response body. Instead of rewrite the parser, use the lib/http to send and receive the request/response. * lib/io: do not use absolute path on dummy Watcher parent SysPath Converting that parameter path to absolute path may cause unpredictable result on module that use it. === Chores * lib/ini: add example of marshal/unmarshaling of section with sub This changes also fix the example of field tag on marshaling the map. * lib/io: add method String to FileState type The String method return the string representation of FileState. Usually used only for debugging. * lib/memfs: remove unnecessary initialization on NewNode The zero value for V ([]byte) is already nil and Node.Childs ([]*Node) does not need to be initialized with make if size is 0. * lib/io: use t.Cleanup instead of defer on test Signed-off-by: Shulhan <ms@kilabit.info>
2021-10-26lib/memfs: move the go embed test to directory internal/testShulhan
This is to remove "embed_test" being displayed on the documentation page.
2021-10-26lib/memfs: move the embedded parameter to OptionsShulhan
Since the GoEmbed can be called only when MemFS instance is initiated, it would be better if parameters for GoEmbed also initialized in the struct Options. In this way any additional parameters needed to add to GoEmbed does not changes the method signature in the future. This commit add new type EmbedOptions that contains the parameters for GoEmbed. In this new type, we add new field EmbedWithoutModTime. if its true, the modification time for all files and directories are not stored inside generated code, instead all files will use the current time when the program is running.
2021-10-25lib/totp: make the New to accept only hash with SHA1, SHA256, or SHA512Shulhan
Previously, the first parameter to New is a function that return hash.Hash. This signature can be misleading, because md5.New also can return hash.Hash but not usable in TOTP. This changes update the New function signature to accept defined type that can be set to SHA1, SHA256, or SHA512.
2021-10-23lib/ini: check for empty string on IsValidVarNameShulhan
2021-10-23lib/ini: add function IsValidVarNameShulhan
The IsValidVarName check if "v" is valid variable name, where the first character must be a letter and the rest should contains only letter, digit, period, hyphen, or underscore. If "v" is valid it will return true.
2021-10-23lib/ini: add example of marshal/unmarshaling of section with subShulhan
This changes also fix the example of field tag on marshaling the map.
2021-10-17lib/bytes: refactoring and cleaning up the bytes packageShulhan
The bytes package, and many other packages in this module, is written when I still learning and using the Go language for master thesis. Some of the code, like function signature, does not follow the Go idiom, at least not how the Go source code looks like. A breaking changes, * WriteUint16 and WriteUint32 accept slice only without pointer. There is no need to pass slice as pointer to function if we want to modify the content, as long as the backing storage is not changed. Bug fixes, * PrintHex: fix print layout on the last line * ReadHexByte: fix possible index out of range * SkipAfterToken return -1 and false if no token found, as promised in the comment, instead of the length of text. We move all unit test to example so we have test and example in the documentation at the same time. This changes make all test coverage 100%.
2021-10-14lib/bytes: refactoring AppendXxx functionsShulhan
Previously, we pass pointer to slice on AppendInt16, AppendInt32, AppendUint16, and AppendUint32 functions. This model of function signature is not a Go idiom. It is written when I am still new to Go.
2021-10-14lib/ascii: change signature of ToLower and ToUpperShulhan
Using pointer to slice on method or function is not a Go idiom. It is created when I still new to Go.
2021-10-14lib/ascii: add examples for all exported functionsShulhan
This is including IsAlnum, IsAlpha, IsDigit, IsDigits, IsHex, IsSpace, and Random.
2021-10-14lib/ascii: fix IsHex return false on 0Shulhan
2021-10-10lib/memfs: fix the embed test file namesShulhan
The variable memFS is exist only on test file, but if the file name for generated GoEmbed is not _test.go the "go install /lib/memfs/..." will thrown an error go install ./... # github.com/shuLhan/share/lib/memfs/embed_test lib/memfs/embed_test/memfs_embed.go:20:31: undefined: memFS lib/memfs/embed_test/memfs_embed.go:21:31: undefined: memFSR
2021-10-09lib/io: fix test on DirWatcherShulhan
On the test we re-create the root directory with sub-directory. This action will trigger two FileStateCreated events, one for root "/" and another one for sub-directory "/assets".
2021-10-09lib/memfs: fix comment on generated Go codeShulhan
The generated comment for _getNode should match with generated function name which has the prefix of variable name.
2021-10-09lib/memfs: remove PathNode "f" fieldShulhan
Previously, the PathNode has two fields to store the node in memory, one is "v" that store map of path to *Node and another is "f" that store the map of path to function that return a *Node. The "f" is used to handle embedding Go generated code, but since the template now can handle generated dynamic path and Node this field is not used anymore.
2021-10-09lib/memfs: prefix all error when the method or function namesShulhan
One of the hardest when multiple packages is used is to detect where there error happened. For example, if a program return an error io.EOF, it's hard to detect the exact method or function that caused its, especially when processing multiple files with network connection.
2021-10-09lib/memfs: use consistent receiver name on Node methodsShulhan
This changes rename the receiver name on Node from "leaf" to "node".
2021-10-09lib/memfs: use temporary directory for testing Node.Save()Shulhan
Previously we use the "testdata" for output of Node.Save() test. This is caused the modified time on "testdata" get changes every time we run the test and cause the embed_test failed. This changes use temporary directory and revert part of Node Save() unit test from commit 792ba17ac1f.
2021-10-09lib/memfs: refactoring Node field V into ContentShulhan
The reason why the field named V is because it's short. It's come from my C/C++ experience that got carried away when writing this package. Now, after having more time writing Go, I prefer clarity over cleverity(?).