Skip to content

Commit

Permalink
Create MyFleetGirls
Browse files Browse the repository at this point in the history
  • Loading branch information
ponkotuy committed Feb 19, 2014
0 parents commit 0eb2ef1
Show file tree
Hide file tree
Showing 15 changed files with 346 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .gitignore
@@ -0,0 +1,18 @@
*.class
*.log

# sbt specific
dist/*
target/
lib_managed/
src_managed/
project/boot/
project/plugins/project/

# Scala-IDE specific
.scala_dependencies
.idea/
.idea_modules/

# Others
*~
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 ぽんこつ

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 changes: 8 additions & 0 deletions README.org
@@ -0,0 +1,8 @@
# -*- coding:utf-8 -*-

#+TITLE: MyFleetGirls
#+AUTHOR: ポンコツ戦艦山本
#+EMAIL: web@ponkotuy.com
#+OPTIONS: toc:nil num:nil author:nil creator:nil
#+STYLE: <link rel="stylesheet" type="text/css" href="org.css"></link>
#+LANGUAGE: ja
14 changes: 14 additions & 0 deletions build.sbt
@@ -0,0 +1,14 @@

name := "MyFleetGirls"

version := "0.0.1-SNAPSHOT"

scalaVersion := "2.10.3"

libraryDependencies ++= Seq(
"com.twitter" %% "finagle-http" % "6.10.0",
"com.github.theon" %% "scala-uri" % "0.3.6",
"org.json4s" %% "json4s-native" % "3.2.6"
)

scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature", "-language:implicitConversions")
2 changes: 2 additions & 0 deletions project/plugin.sbt
@@ -0,0 +1,2 @@

addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")
56 changes: 56 additions & 0 deletions src/main/scala/com/ponkotuy/data/Basic.scala
@@ -0,0 +1,56 @@
package com.ponkotuy.data

import org.json4s.JsonAST.JValue
import org.json4s.DefaultFormats

/**
*
* @param lv 艦隊司令部Lv
* @param experience 艦隊司令部経験値
* @param rank 階級
* @param maxChara 艦娘保有上限
* @param fCoin 家具コイン
* @param stWin 出撃勝敗
* @param msWin 遠征の成功失敗
* @param ptWin 演習勝敗
* @author ponkotuy
* Date: 14/02/20
*/
case class Basic(lv: Int, experience: Int, rank: Int, maxChara: Int, fCoin: Int, stWin: WinLose, msWin: WinLose, ptWin: WinLose)
object Basic {
implicit val formats = DefaultFormats

def fromJSON(json: JValue): Basic = {
implicit def toInt(json: JValue) = json.extract[Int]
val lv = json \ "api_level"
val experience = json \ "api_experience"
val rank = json \ "api_rank"
val maxChara = json \ "api_max_chara"
val fCoin = json \ "api_fcoin"
val stWin = WinLose(json \ "api_st_win", json \ "api_st_lose")
val msCount = json \ "api_ms_count"
val msSuccess = json \ "api_ms_success"
val msWin = WinLose(msSuccess, msCount - msSuccess)
val ptWin = WinLose(json \ "api_pt_win", json \ "api_pt_lose")
Basic(lv, experience, rank, maxChara, fCoin, stWin, msWin, ptWin)
}
}

case class WinLose(win: Int, lose: Int)

/** このツール内でログイン代わりに使うパラメータ
*
* @param id nick name id
* @param startTime ゲーム開始時間っぽいけど暗号代わりに
*/
case class Auth(id: Long, nickname: String, startTime: Long)
object Auth {
implicit val formats = DefaultFormats

def fromJSON(json: JValue): Auth = {
val id = (json \ "api_nickname_id").extract[String]
val nickname = (json \ "api_nickname").extract[String]
val startTime = (json \ "api_starttime").extract[Long]
Auth(id.toLong, nickname, startTime)
}
}
16 changes: 16 additions & 0 deletions src/main/scala/com/ponkotuy/data/Material.scala
@@ -0,0 +1,16 @@
package com.ponkotuy.data

/**
*
*
* @param instant : Instant Construction
* @param develop : Development Material
* @author ponkotuy
* Date: 14/02/19.
*/
case class Material(fuel: Int, ammo: Int, steel: Int, bauxite: Int, instant: Int, bucket: Int, develop: Int)
object Material {
def fromSeq(s: Seq[Int]): Material = {
Material(s(0), s(1), s(2), s(3), s(4), s(5), s(6))
}
}
12 changes: 12 additions & 0 deletions src/main/scala/com/ponkotuy/intercept/Intercepter.scala
@@ -0,0 +1,12 @@
package com.ponkotuy.intercept

import org.jboss.netty.handler.codec.http.{HttpResponse, HttpRequest}

/**
*
* @author ponkotuy
* Date: 14/02/18.
*/
trait Intercepter {
def input(req: HttpRequest, res: HttpResponse): Unit
}
30 changes: 30 additions & 0 deletions src/main/scala/com/ponkotuy/intercept/KCIntercepter.scala
@@ -0,0 +1,30 @@
package com.ponkotuy.intercept

import scala.collection.JavaConverters._
import org.jboss.netty.handler.codec.http.{HttpResponse, HttpRequest}
import com.ponkotuy.parser.{KCJson, ResType}
import java.nio.charset.Charset

/**
*
* @author ponkotuy
* Date 14/02/19.
*/
class KCIntercepter extends Intercepter {
import KCIntercepter._
override def input(req: HttpRequest, res: HttpResponse): Unit = {
val restype = ResType.fromUri(req.getUri)
for {
typ <- restype
headers = entries4s(req.headers().entries())
json <- KCJson.toAst(res.getContent.toString(Charset.forName("UTF-8")))
} {
typ.run(headers, json)
}
}
}

object KCIntercepter {
def entries4s[K, V](entries: java.util.List[java.util.Map.Entry[K, V]]): Map[K, V] =
entries.asScala.map { entry => entry.getKey -> entry.getValue }.toMap
}
12 changes: 12 additions & 0 deletions src/main/scala/com/ponkotuy/intercept/PassThrough.scala
@@ -0,0 +1,12 @@
package com.ponkotuy.intercept

import org.jboss.netty.handler.codec.http.{HttpResponse, HttpRequest}

/**
*
* @author ponkotuy
* Date: 14/02/18.
*/
class PassThrough extends Intercepter {
override def input(req: HttpRequest, res: HttpResponse): Unit = {}
}
37 changes: 37 additions & 0 deletions src/main/scala/com/ponkotuy/intercept/PrintContent.scala
@@ -0,0 +1,37 @@
package com.ponkotuy.intercept

import java.nio.charset.Charset
import org.jboss.netty.handler.codec.http.{HttpResponse, HttpRequest}

/**
*
* @author ponkotuy
* Date: 14/02/18.
*/
class PrintContent extends Intercepter {
def response(res: HttpResponse): Unit = {
println("Arrived Response")
println(s"Status: ${res.getStatus}")
println(s"Headers: ${res.headers().entries()}")
if(res.headers().get("Content-Type") == "text/plain") {
println(s"content: ${res.getContent.toString(Charset.forName("UTF-8"))}")
} else {
println("Binary(not text).")
}
}

def request(req: HttpRequest): Unit = {
println("Sending Request")
println(Map(
"uri" -> req.getUri,
"method" -> req.getMethod,
"header" -> req.headers().entries(),
"content" -> req.getContent.toString(Charset.forName("UTF-8"))
))
}

override def input(req: HttpRequest, res: HttpResponse): Unit = {
request(req)
response(res)
}
}
23 changes: 23 additions & 0 deletions src/main/scala/com/ponkotuy/parser/KCJson.scala
@@ -0,0 +1,23 @@
package com.ponkotuy.parser

import org.json4s._
import org.json4s.native.JsonMethods._
import scala.util.Try
import org.json4s.native.Serialization

/**
*
* @author ponkotuy
* Date 14/02/19.
*/
object KCJson {
implicit val formats = Serialization.formats(NoTypeHints)

def toAst(content: String): Option[JValue] = {
val ast = parse(content.replaceFirst("svdata=", ""))
Try {
assert((ast \ "api_result").extract[Int] == 1)
ast \ "api_data"
}.toOption
}
}
46 changes: 46 additions & 0 deletions src/main/scala/com/ponkotuy/parser/ResType.scala
@@ -0,0 +1,46 @@
package com.ponkotuy.parser

import com.github.theon.uri.Uri
import org.json4s._
import org.json4s.native.Serialization
import com.ponkotuy.data


/**
*
* @author ponkotuy
* Date: 14/02/19.
*/
sealed abstract class ResType(val path: String) {
def run(reqHeaders: Map[String, String], obj: JValue): Unit
}

object ResType {
implicit val formats = Serialization.formats(NoTypeHints)

val GetMember = "/kcsapi/api_get_member"

case object Material extends ResType(s"$GetMember/material") {
override def run(reqHeaders: Map[String, String], obj: JValue): Unit = {
val xs: Seq[Int] = (obj \\ "api_value").children.map(_.extract[Int])
val material = data.Material.fromSeq(xs)
println(material)
}
}

case object Basic extends ResType(s"$GetMember/basic") {
override def run(reqHeaders: Map[String, String], obj: JValue): Unit = {
val basic = data.Basic.fromJSON(obj)
val auth = data.Auth.fromJSON(obj)
println(basic, auth)
}
}

val values = Set(Material, Basic)

def fromUri(uri: String): Option[ResType] = {
val path = Uri.parseUri(uri).pathRaw
println(path)
values.find(_.path == path)
}
}
39 changes: 39 additions & 0 deletions src/main/scala/com/ponkotuy/proxy/FinagleProxy.scala
@@ -0,0 +1,39 @@
package com.ponkotuy.proxy

import com.github.theon.uri.Uri
import com.twitter.finagle.builder.ClientBuilder
import com.twitter.finagle.{http, Service, Http}
import com.twitter.util.{Future, Await}
import org.jboss.netty.handler.codec.http.{HttpMethod, HttpResponse, HttpRequest}
import com.ponkotuy.intercept.{Intercepter, PassThrough}

/** Proxy by Finagle
*
* @param hosts: Require Port Number(hostname:80 etc...)
* @param inter: com.ponkotuy.intercept.Intercepter
*/
class FinagleProxy(hosts: String, inter: Intercepter = new PassThrough) {
val client = ClientBuilder()
.codec(http.Http())
.hosts("125.6.189.39:80")
.hostConnectionLimit(4).build()

val service = new Service[HttpRequest, HttpResponse] {
def apply(req: HttpRequest): Future[HttpResponse] = {
val res = if(req.getMethod == HttpMethod.POST) {
val uri = Uri.parseUri(req.getUri)
req.setUri(uri.pathRaw)
client.apply(req)
} else {
client.apply(req)
}
res.foreach(rs => inter.input(req, rs))
res
}
}
val server = Http.serve(":8080", service)

def start(): Unit = {
Await.ready(server)
}
}
13 changes: 13 additions & 0 deletions src/main/scala/com/ponkotuy/run/Main.scala
@@ -0,0 +1,13 @@
package com.ponkotuy.run

import com.ponkotuy.intercept.{KCIntercepter, PrintContent}
import com.ponkotuy.proxy.FinagleProxy

/**
*
* @author ponkotuy
* Date: 14/02/18.
*/
object Main extends App {
new FinagleProxy("125.6.189.39", new KCIntercepter).start()
}

0 comments on commit 0eb2ef1

Please sign in to comment.