Better code organization, reusability

This commit is contained in:
Carl Johnson 2016-12-10 23:14:03 -05:00
parent 7997e6373e
commit 5afbb7f3e9
3 changed files with 53 additions and 33 deletions

View File

@ -1,9 +1,15 @@
package heff
import "net/http"
import (
"io"
"log"
"net/http"
)
func Honeypot(w http.ResponseWriter, r *http.Request) {
for {
Generate(w, 10000)
}
log.Printf("Started writing: %v", r.URL)
buf := make([]byte, 100*1<<10)
io.WriteString(w, "<HTML>\n<BODY>\n")
n, err := io.CopyBuffer(w, DefaultMarkovMap, buf)
log.Printf("Wrote: %d (%v)", n, err)
}

View File

@ -2,7 +2,6 @@ package heff
import (
"bufio"
"fmt"
"io"
"math/rand"
"strings"
@ -52,44 +51,60 @@ func ScanHTML(data []byte, atEOF bool) (advance int, token []byte, err error) {
return start, nil, nil
}
const (
nprefix = 2
nonword = "\n"
)
type tokenPair [2]string
type tokenPair [nprefix]string
var DefaultMarkovMap = MakeMarkovMap(strings.NewReader(Src))
var markov = make(map[tokenPair][]string)
type MarkovMap map[tokenPair][]string
func init() {
var w1, w2 = nonword, nonword
var p tokenPair
func MakeMarkovMap(r io.Reader) MarkovMap {
m := MarkovMap{}
m.Fill(r)
return m
}
s := bufio.NewScanner(strings.NewReader(Src))
func (mm MarkovMap) Fill(r io.Reader) {
var w1, w2, w3 string
s := bufio.NewScanner(r)
s.Split(ScanHTML)
for s.Scan() {
t := s.Text()
p = tokenPair{w1, w2}
markov[p] = append(markov[p], t)
w1, w2 = w2, t
w3 := s.Text()
mm.Add(w1, w2, w3)
w1, w2 = w2, w3
}
p = tokenPair{w1, w2}
markov[p] = append(markov[p], nonword)
mm.Add(w1, w2, w3)
}
func Generate(w io.Writer, maxgen int) {
var w1, w2 = nonword, nonword
func (mm MarkovMap) Add(w1, w2, w3 string) {
p := tokenPair{w1, w2}
mm[p] = append(mm[p], w3)
}
for i := 0; i < maxgen; i++ {
p := tokenPair{w1, w2}
suffix := markov[p]
r := rand.Intn(len(suffix))
t := suffix[r]
if t == nonword {
func (mm MarkovMap) Get(w1, w2 string) string {
p := tokenPair{w1, w2}
suffix, ok := mm[p]
if !ok {
return ""
}
r := rand.Intn(len(suffix))
return suffix[r]
}
func (mm MarkovMap) Read(p []byte) (n int, err error) {
var w1, w2, w3 string
for {
w3 = mm.Get(w1, w2)
if n+len(w3)+1 >= len(p) {
break
}
fmt.Fprint(w, t, "\n")
w1, w2 = w2, t
n += copy(p[n:], w3)
n += copy(p[n:], "\n")
w1, w2 = w2, w3
}
return
}

View File

@ -1,7 +1,6 @@
package heff
const Src = `<HTML><BODY>
<H1 ALIGN="center">
const Src = `<H1 ALIGN="center">
ONCE ON A TIME
</H1>