@@ -14,7 +14,12 @@ const sequelize = new Sequelize('sqlite::memory:', {
1414
1515const Item = sequelize . define ( "Item" , {
1616 sha : { type : DataTypes . STRING , allowNull : false } ,
17- ip : { type : DataTypes . STRING , allowNull : false }
17+ ip : { type : DataTypes . STRING , allowNull : false } ,
18+ headers : { type : DataTypes . STRING , allowNull : true } ,
19+ graphics : { type : DataTypes . STRING , allowNull : true } ,
20+ memory : { type : DataTypes . STRING , allowNull : true } ,
21+ platform : { type : DataTypes . STRING , allowNull : true } ,
22+ hardwareConcurrency : { type : DataTypes . STRING , allowNull : true } ,
1823} )
1924
2025const {
@@ -106,14 +111,23 @@ app.get('/add-sha/:sha', (req, res) => {
106111} )
107112
108113app . get ( '/collect-sha/:sha' , async ( req , res ) => {
109- const ip = req . headers [ 'x-forwarded-for' ]
110- const item = await Item . findOne ( { where : { sha : req . params . sha } } )
111- if ( item !== null ) {
112- return res . json ( { message : "visited" } )
113- } else {
114- console . log ( JSON . stringify ( item , null , 2 ) )
115- const newItem = await Item . create ( { sha : req . params . sha , ip : ip } )
116- return res . json ( { item : newItem } )
114+ try {
115+ const ip = req . headers [ 'x-forwarded-for' ]
116+ const metadata = JSON . parse ( Base64 . decode ( req . headers [ 'metadata' ] ) )
117+ const { headers, isp, memory, platform, hardwareConcurrency, graphics} = metadata
118+ const item = await Item . findOne ( { where : { sha : req . params . sha } } )
119+ if ( item !== null ) {
120+ return res . json ( { message : "visited" } )
121+ } else {
122+ console . log ( JSON . stringify ( item , null , 2 ) )
123+ const newItem = await Item . create ( {
124+ sha : req . params . sha ,
125+ ip, headers, isp, platform, memory, hardwareConcurrency, graphics
126+ } )
127+ return res . json ( { item : newItem } )
128+ }
129+ } catch {
130+ return res . status ( 422 ) . json ( { message : "Something Went Wrong" } )
117131 }
118132} )
119133
0 commit comments