├── .gitignore ├── options.go ├── .github └── FUNDING.yml ├── util.go ├── client.go ├── validate.go ├── check.go ├── vars.go ├── main.go ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | dist/* -------------------------------------------------------------------------------- /options.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | type options struct { 4 | reg string 5 | dep string 6 | list []string 7 | } 8 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: ["dwisiswant0"] 2 | custom: ["https://paypal.me/dw1s", "https://saweria.co/dwisiswant0", "https://unstoppabledomains.com/d/dwisiswant0.crypto"] 3 | -------------------------------------------------------------------------------- /util.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "os" 6 | ) 7 | 8 | func readFile(path string) ([]string, error) { 9 | var lines []string 10 | 11 | file, err := os.Open(path) 12 | if err != nil { 13 | return nil, err 14 | } 15 | defer file.Close() 16 | 17 | scanner := bufio.NewScanner(file) 18 | for scanner.Scan() { 19 | lines = append(lines, scanner.Text()) 20 | } 21 | return lines, scanner.Err() 22 | } 23 | -------------------------------------------------------------------------------- /client.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net" 5 | "net/http" 6 | "time" 7 | ) 8 | 9 | func client() *http.Client { 10 | tr := &http.Transport{ 11 | MaxIdleConns: 30, 12 | IdleConnTimeout: time.Second, 13 | DialContext: (&net.Dialer{ 14 | Timeout: time.Second * 30, 15 | KeepAlive: time.Second, 16 | }).DialContext, 17 | } 18 | 19 | re := func(req *http.Request, via []*http.Request) error { 20 | return http.ErrUseLastResponse 21 | } 22 | 23 | return &http.Client{ 24 | Transport: tr, 25 | CheckRedirect: re, 26 | Timeout: time.Second * 30, 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /validate.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | ) 7 | 8 | func (opt *options) validate() error { 9 | if opt.reg == "" { 10 | return errors.New("no registry given") 11 | } 12 | 13 | switch opt.reg { 14 | case "npm", "pip", "gem": 15 | default: 16 | return fmt.Errorf("invalid registry for '%s'", opt.reg) 17 | } 18 | 19 | if opt.dep == "" { 20 | return errors.New("no dependencies given") 21 | } 22 | 23 | if list, err := readFile(opt.dep); err == nil { 24 | opt.list = list 25 | } else { 26 | opt.list = []string{opt.dep} 27 | } 28 | 29 | return nil 30 | } 31 | -------------------------------------------------------------------------------- /check.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func check(reg string, dep string) (bool, error) { 6 | switch reg { 7 | case "npm": 8 | endpoint = fmt.Sprintf("https://api.npms.io/v2/package/%s/", dep) 9 | case "pip": 10 | endpoint = fmt.Sprintf("https://pypi.org/simple/%s/", dep) 11 | case "gem": 12 | endpoint = fmt.Sprintf("https://rubygems.org/api/v1/gems/%s.json", dep) 13 | default: 14 | return false, nil 15 | } 16 | 17 | resp, err := do.Get(endpoint) 18 | if err != nil { 19 | return false, err 20 | } 21 | 22 | if resp.StatusCode == 200 { 23 | return true, nil 24 | } 25 | 26 | return false, nil 27 | } 28 | -------------------------------------------------------------------------------- /vars.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net/http" 5 | "sync" 6 | ) 7 | 8 | var ( 9 | endpoint string 10 | 11 | cpre = "\033[" 12 | cend = cpre + "0m" 13 | cred = cpre + "31m" 14 | cgrn = cpre + "32m" 15 | cyel = cpre + "33m" 16 | 17 | do *http.Client 18 | wg sync.WaitGroup 19 | opt *options 20 | 21 | usage = `nodep — check available dependency packages across npmjs, PyPI or RubyGems registry. 22 | by @dwisiswant0 23 | 24 | Usage: 25 | nodep 26 | 27 | Available registry options: 28 | - npm (npmjs) 29 | - pip (PyPI) 30 | - gem (RubyGems) 31 | 32 | Examples: 33 | nodep pip reqeusts 34 | nodep npm package.txt 35 | ` 36 | ) 37 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "os" 7 | ) 8 | 9 | func init() { 10 | flag.Usage = func() { 11 | fmt.Fprint(os.Stderr, usage) 12 | } 13 | flag.Parse() 14 | 15 | opt = &options{} 16 | opt.reg = flag.Arg(0) 17 | opt.dep = flag.Arg(1) 18 | 19 | if err := opt.validate(); err != nil { 20 | fmt.Fprintf(os.Stderr, "%sError: %s!%s\n\n%s", cred, err, cend, usage) 21 | os.Exit(1) 22 | } 23 | 24 | do = client() 25 | } 26 | 27 | func main() { 28 | wg.Add(1) 29 | 30 | go func() { 31 | for _, dep := range opt.list { 32 | reg, err := check(opt.reg, dep) 33 | if err != nil { 34 | fmt.Printf("%s[error] %s%s\n", cyel, cend, dep) 35 | } 36 | 37 | if reg { 38 | fmt.Printf("%s[regis] %s%s\n", cred, cend, dep) 39 | } else { 40 | fmt.Printf("%s[avail] %s%s\n", cgrn, cend, dep) 41 | } 42 | } 43 | 44 | defer wg.Done() 45 | }() 46 | 47 | wg.Wait() 48 | } 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ``` 2 | nodep — check available dependency packages across npmjs, PyPI or RubyGems registry. 3 | 4 | Installation (with Go): 5 | $ go get -u github.com/dwisiswant0/nodep 6 | 7 | Or download pre-built binary from releases page (https://github.com/dwisiswant0/nodep/releases/latest). 8 | 9 | Usage of nodep: 10 | nodep 11 | 12 | Available registry options: 13 | - npm (npmjs) 14 | - pip (PyPI) 15 | - gem (RubyGems) 16 | 17 | Examples: 18 | nodep pip reqeusts 19 | nodep npm package.txt 20 | 21 | Supporting Materials: 22 | - Birsan, Alex. “Dependency Confusion: How I Hacked Into Apple, Microsoft and Dozens of Other Companies.” Medium, February 9, 2021, https://medium.com/@alex.birsan/dependency-confusion-4a5d60fec610. 23 | - Tschacher, Nikolai. “Typosquatting Programming Language Package Managers.” incolumitas.com, June 8, 2016, https://incolumitas.com/2016/06/08/typosquatting-package-managers/. 24 | ``` -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 dw1 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------