Skip to content

Instantly share code, notes, and snippets.

@tetheredge
Created June 23, 2016 01:56
Show Gist options
  • Save tetheredge/3a74c7a61c82ac0ee2b0572dcf0cb609 to your computer and use it in GitHub Desktop.
Save tetheredge/3a74c7a61c82ac0ee2b0572dcf0cb609 to your computer and use it in GitHub Desktop.
nvd_update_code
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"io"
"log"
"net/http"
"os"
"time"
)
const modifiedUrl string = "https://nvd.nist.gov/download/nvdcve-Modified.xml.gz"
const modifiedFilePath string = "nvdcve-Modified.xml.gz"
const metaUrl string = "https://nvd.nist.gov/download/nvdcve-Modified.meta"
const latestMetaFilepath string = "latestNvdcve-Modified.meta"
const metaFile string = "nvdcve-Modified.meta"
type Download struct {
}
func (dw Download) GetFile(filePath, url string) (err error) {
out, err := os.Create(filePath)
if err != nil {
return err
}
defer out.Close()
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func (dw Download) GetModifiedFile(name, modifiedUrl string) error {
os.Chdir("nvd_files")
out, err := os.Create(name + "-Modified.xml.gz")
if err != nil {
return err
}
resp, err := http.Get(modifiedUrl)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func main() {
download := Download{}
err = download.GetFile("../nvd-files/"+latestMetaFilepath, metaUrl)
CheckError(err)
fileToString, err := config.GetFile("../nvd-files", latestMetaFilepath)
CheckError(err)
latestModifiedTime, err := config.ParseMetaFile(fileToString)
CheckError(err)
fileToString, err = config.GetFile("../nvd-files", metaFile)
CheckError(err)
modifiedTime, err := config.ParseMetaFile(fileToString)
CheckError(err)
if latestModifiedTime.After(modifiedTime) {
os.Rename(latestMetaFilepath, metaFile)
err := download.GetModifiedFile("nvdcve", modifiedUrl)
CheckError(err)
config, err = config.XmlDecompose(modifiedFilePath)
CheckError(err)
} else {
log.Println("There are not any updates to import")
os.Exit(0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment