Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- // TODO:
- // 1)Сделать сохранение всех данных из формы с преобразованиями - done
- // 2)Сделать окошко с количеством отправляемых пакетов
- // 3)Подключить девайс и затестить, правильно ли отправляется пакет
- import (
- "fmt"
- "html/template"
- "os"
- "log"
- "net"
- "net/http"
- "time"
- "github.com/google/gopacket"
- "github.com/google/gopacket/pcap"
- "github.com/google/gopacket/layers"
- )
- var (
- //All templates ptr
- tpl *template.Template
- //All interfaces array
- interfArray []net.Interface
- //Chosen interface
- chosenInterf net.Interface
- //Packet laeyrs
- ipLayer layers.IPv4
- ethLayer layers.Ethernet
- tcpLayer layers.TCP
- icmpLayer layers.ICMPv4
- udpLayer layers.UDP
- //Error variable
- err error
- )
- func init() {
- //TODO: Parse templates only once - in init function
- //tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
- interfArray, err = net.Interfaces()
- if err != nil {
- log.Fatal(err)
- }
- }
- func networkLayer(w http.ResponseWriter, req *http.Request) {
- tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
- sendStruct := struct {
- Interfaces []net.Interface
- Protocol []layers.IPProtocol
- EtherType []layers.EthernetType
- }{
- Interfaces: interfArray,
- Protocol: Protocols,
- EtherType: EtherTypes,
- }
- err = tpl.ExecuteTemplate(w, "netlayer.gohtml", sendStruct)
- if err != nil {
- log.Fatal(err)
- }
- }
- func changeChosenInterf(name string) {
- for _, i := range interfArray {
- if i.Name == name {
- chosenInterf = i
- }
- }
- }
- func saveIPLayer(w http.ResponseWriter, req *http.Request) {
- ipLayer.Version = StringToUint8(req.FormValue("versionInput"))
- ipLayer.IHL = StringToUint8(req.FormValue("ihlInput"))
- // fmt.Printf("%+v", ipLayer)
- // os.Exit(1)
- ipLayer.TOS = StringToUint8(req.FormValue("tosInput"))
- ipLayer.Length = StringToUint16(req.FormValue("lengthInput"))
- ipLayer.Id = StringToUint16(req.FormValue("idInput"))
- ipLayer.Flags = SetIPFlag(req.FormValue("flagInput"))
- ipLayer.FragOffset = StringToUint16(req.FormValue("fragOffInput"))
- ipLayer.TTL = StringToUint8(req.FormValue("ttlInput"))
- ipLayer.Protocol = SetIPProtocol(req.FormValue("protocolInput"))
- ipLayer.Checksum = StringToUint16(req.FormValue("checksumInput"))
- ipLayer.DstIP = net.ParseIP(req.FormValue("dstInput"))
- ipLayer.SrcIP = net.ParseIP(req.FormValue("srcInput"))
- }
- func saveEthernetLayer(w http.ResponseWriter, req *http.Request) {
- if req.FormValue("srcMac") == "" {
- ethLayer.SrcMAC = AutoMAC(ipLayer.SrcIP, interfArray)
- } else {
- ethLayer.SrcMAC = StringToHardAddr(req.FormValue("srcMac"))
- }
- if req.FormValue("dstMac") == "" {
- ethLayer.DstMAC = AutoMAC(ipLayer.DstIP, interfArray)
- } else {
- ethLayer.DstMAC = StringToHardAddr(req.FormValue("dstMac"))
- }
- ethLayer.EthernetType = SetEthType(req.FormValue("etherType"))
- ethLayer.Length = StringToUint16(req.FormValue("etherLength"))
- changeChosenInterf(req.FormValue("networkDeviceInput"))
- }
- func saveData(w http.ResponseWriter, req *http.Request) {
- saveIPLayer(w, req)
- saveEthernetLayer(w, req)
- if ipLayer.Protocol.String() == "TCP" {
- tcpLayer = SaveTCPLayer(w, req)
- } else if ipLayer.Protocol.String() == "UDP" {
- udpLayer = SaveUDPLayer(w, req)
- } else if ipLayer.Protocol.String() == "ICMPv4" {
- icmpLayer = SaveICMPLayer(w, req)
- }
- generatePacket()
- }
- func generatePacket() {
- handle, err := pcap.OpenLive(chosenInterf.Name,
- 1024, false, 30*time.Second)
- if err != nil {
- log.Fatal(err)
- }
- var options gopacket.SerializeOptions
- buffer := gopacket.NewSerializeBuffer()
- if ipLayer.Protocol.String() == "TCP" {
- gopacket.SerializeLayers(buffer, options, ðLayer, &ipLayer,
- &tcpLayer, gopacket.Payload([]byte("hello there world")))
- } else if ipLayer.Protocol.String() == "UDP" {
- gopacket.SerializeLayers(buffer, options, ðLayer, &ipLayer,
- &udpLayer, gopacket.Payload([]byte("hello there world")))
- } else if ipLayer.Protocol.String() == "ICMPv4" {
- //fmt.Printf("%+v", icmpLayer)
- fmt.Printf("%+v", ipLayer)
- gopacket.SerializeLayers(buffer, options, ðLayer,
- &ipLayer,
- &icmpLayer,
- gopacket.Payload([]byte("HELLO-R-U-THERE")))
- }
- outgoingpacket := buffer.Bytes()
- err = handle.WritePacketData(outgoingpacket)
- if err != nil {
- log.Fatal(err)
- }
- }
- func main() {
- http.HandleFunc("/", networkLayer)
- http.HandleFunc("/savedata", saveData)
- http.ListenAndServe(":"+os.Args[1], nil)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement