Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "crypto/sha256"
- "fmt"
- "io"
- "log"
- "net/http"
- "os"
- "regexp"
- "strings"
- "github.com/PuerkitoBio/goquery"
- )
- type arch struct {
- title string
- sha256sum string
- }
- const repoURL = "https://repo.nordvpn.com/deb/nordvpn/debian/pool/main/"
- var packageRegex = regexp.MustCompile(`^nordvpn_(.+)_(.+)\.deb$`)
- func main() {
- // Download Goquery document
- doc, err := downloadAsGoqueryDocument(repoURL)
- if err != nil {
- log.Fatal(err)
- }
- // Find last A element, so we can understand what is the latest version from it
- el := doc.Find(`a[href$=".deb"][href^="nordvpn_"]`).Last()
- if el == nil {
- log.Fatal("unable to read packages from the repo document")
- }
- packageTitle, _ := el.Attr("href")
- // Read version from that package name
- regexExtractArray := packageRegex.FindStringSubmatch(packageTitle)
- if len(regexExtractArray) != 3 {
- log.Fatal("regex is broken and needs to be fixed")
- }
- version := regexExtractArray[1]
- // Find all available architectures and create slice of 'arch'
- architectures := findArchitectures(doc, version)
- // Fill sha256sum checksums for each 'arch'
- fillChecksums(&architectures, &version)
- // Print PKGBUILD file content
- printPKGBUILD(&architectures, &version)
- }
- func printPKGBUILD(architectures *[]arch, version *string) {
- // First we generate variables
- var sourceLines, sha256sumLines string
- archLine := "arch=("
- for _, architecture := range *architectures {
- switch architecture.title {
- case "i386":
- archLine += "'i686' "
- sourceLines += "source_i686=(\"" + repoURL + "nordvpn_" + *version + "_" + architecture.title + ".deb\")\n"
- sha256sumLines += "sha256sums_i686=(\"" + architecture.sha256sum + "\")\n"
- case "amd64":
- archLine += "'x86_64' "
- sourceLines += "source_x86_64=(\"" + repoURL + "nordvpn_" + *version + "_" + architecture.title + ".deb\")\n"
- sha256sumLines += "sha256sums_x86_64=(\"" + architecture.sha256sum + "\")\n"
- case "armhf":
- archLine += "'armv7h' "
- sourceLines += "source_armv7h=(\"" + repoURL + "nordvpn_" + *version + "_" + architecture.title + ".deb\")\n"
- sha256sumLines += "sha256sums_armv7h=(\"" + architecture.sha256sum + "\")\n"
- case "armel":
- archLine += "'armv6h' "
- sourceLines += "source_armv6h=(\"" + repoURL + "nordvpn_" + *version + "_" + architecture.title + ".deb\")\n"
- sha256sumLines += "sha256sums_armv6h=(\"" + architecture.sha256sum + "\")\n"
- archLine += "'arm' "
- sourceLines += "source_arm=(\"" + repoURL + "nordvpn_" + *version + "_" + architecture.title + ".deb\")\n"
- sha256sumLines += "sha256sums_arm=(\"" + architecture.sha256sum + "\")\n"
- case "arm64":
- archLine += "'aarch64' "
- sourceLines += "source_aarch64=(\"" + repoURL + "nordvpn_" + *version + "_" + architecture.title + ".deb\")\n"
- sha256sumLines += "sha256sums_aarch64=(\"" + architecture.sha256sum + "\")\n"
- default:
- archLine += "'" + architecture.title + "' "
- sourceLines += "source_" + architecture.title + "=(\"" + repoURL + "nordvpn_" + *version + "_" + architecture.title + ".deb\")\n"
- sha256sumLines += "sha256sums_" + architecture.title + "=(\"" + architecture.sha256sum + "\")\n"
- }
- }
- archLine = archLine[:len(archLine)-1] + ")"
- sourceLines = sourceLines[:len(sourceLines)-1]
- sha256sumLines = sha256sumLines[:len(sha256sumLines)-1]
- // Now we print everything:
- fmt.Printf(`# Maintainer: metiis <aur at metiis dot com>
- # Maintainer: Julio Gutierrez <bubuntux at gmail dot com>
- # Maintainer: Martoko <mbastholm at gmail dot com>
- pkgname=nordvpn-cli
- pkgver=%s
- pkgrel=1
- pkgdesc="Official NordVPN CLI for Linux"
- %s
- url="https://nordvpn.com/download/linux/"
- license=('custom')
- depends=('net-tools' 'libxslt' 'iptables' 'procps' 'iproute2')
- optdepends=('wireguard-tools: nordlynx support' 'wireguard-module: nordlynx support')
- provides=('nordvpn')
- conflicts=('openvpn-nordvpn')
- install=nordvpn-cli.install
- %s
- %s
- package() {
- bsdtar -O -xf *.deb data.tar.xz | bsdtar -C "${pkgdir}" -xJf -
- mv "${pkgdir}/usr/sbin/nordvpnd" "${pkgdir}/usr/bin"
- rm -r "${pkgdir}/etc/init.d"
- rm -r "${pkgdir}/usr/sbin"
- }
- `, strings.ReplaceAll(*version, "-", "_"), archLine, sourceLines, sha256sumLines)
- }
- func fillChecksums(architectures *[]arch, version *string) {
- for i := 0; i < len(*architectures); i++ {
- // Construct titles/paths:
- fileName := "nordvpn_" + *version + "_" + (*architectures)[i].title + ".deb"
- filePath := "/tmp/" + fileName
- // Download file to tmp
- err := downloadFile(filePath, repoURL+fileName)
- if err != nil {
- log.Fatal(err)
- }
- // Calculate sha256sum
- f, err := os.Open(filePath)
- if err != nil {
- log.Fatal(err)
- }
- defer f.Close()
- h := sha256.New()
- if _, err := io.Copy(h, f); err != nil {
- log.Fatal(err)
- }
- (*architectures)[i].sha256sum = fmt.Sprintf("%x", h.Sum(nil))
- // Delete file from tmp
- err = os.Remove(filePath)
- if err != nil {
- log.Fatal(err)
- }
- }
- }
- func findArchitectures(doc *goquery.Document, version string) (availablePackages []arch) {
- doc.Find(`a[href$=".deb"][href^="nordvpn_` + version + `"]`).Each(func(i int, s *goquery.Selection) {
- title, _ := s.Attr("href")
- arr := packageRegex.FindStringSubmatch(title)
- if len(arr) != 3 {
- log.Fatal("regex is broken and needs to be fixed")
- }
- availablePackages = append(availablePackages, arch{title: arr[2]})
- })
- return
- }
- func downloadAsGoqueryDocument(url string) (*goquery.Document, error) {
- res, err := http.Get(url)
- if err != nil {
- return nil, err
- }
- defer res.Body.Close()
- if res.StatusCode != 200 {
- return nil, fmt.Errorf("status code error: %d %s", res.StatusCode, res.Status)
- }
- return goquery.NewDocumentFromReader(res.Body)
- }
- func downloadFile(filepath string, url string) error {
- // Get the data
- resp, err := http.Get(url)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- // Create the file
- out, err := os.Create(filepath)
- if err != nil {
- return err
- }
- defer out.Close()
- // Write the body to file
- _, err = io.Copy(out, resp.Body)
- return err
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement